Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

node.js - Node v6 failing on object spread

I had a question about why node v6.7 would be failing to run this code:

var a = {
    foo: 'bar'
}

var b = {
    ...a,
    my: 'sharona'
}
console.log(b)

Anyone have an idea why that would be? I thought v6 supported object spreading..? But I guess not? Here is the error I'm seeing:

/home/teselagen/ve/tnrtest.js:6
    ...a,
    ^^^
SyntaxError: Unexpected token ...
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:528:28)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using rest/spread with objects is a separate proposal, which you can read about here. A proposal doesn't get accepted for the yearly ES release unless it reaches stage 4, and it is currently stage 3. It may make it into ES2018. If you want to use it now, you'll have to use a transpiler like babel.


EDIT: As of Node v8.3, object rest/spread is available without the need for any transpilation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...