node.js Node v6 在对象传播上失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40066731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:45:47  来源:igfitidea点击:

Node v6 failing on object spread

node.jsecmascript-6

提问by majorBummer

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

我有一个关于为什么 node v6.7 无法运行此代码的问题:

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:

任何人都知道为什么会这样?我认为 v6 支持对象传播..?但我猜不是?这是我看到的错误:

/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

采纳答案by slebetman

Looks like ES6 spread operator only works for arrays and iterables. It is specifically designed to NOT WORKfor objects: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator

看起来 ES6 扩展运算符仅适用于数组和可迭代对象。它专门设计为不适用于对象:https: //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator

Relevant quote:

相关报价:

Only apply for iterables

var obj = {"key1":"value1"};
function myFunction(x) {
    console.log(x); // undefined
}
myFunction(...obj);
var args = [...obj];
console.log(args, args.length) //[] 0

仅适用于可迭代对象

var obj = {"key1":"value1"};
function myFunction(x) {
    console.log(x); // undefined
}
myFunction(...obj);
var args = [...obj];
console.log(args, args.length) //[] 0

Though the MDN article previously suggested that trying to use the spread operator on objects should result in undefined instead of throwing an error. As of this revision, the current MDN article discusses support for "Spread for object literals"

尽管 MDN 文章之前建议尝试在对象上使用扩展运算符应该导致 undefined 而不是抛出错误。截至本次修订,当前的 MDN 文章讨论了对“扩展对象文字”的支持

Additionally the node.js compatibility table claims node.js fully comply with the specification of the spread operator with arrays and iterables, but specifically indicates that object rest/spread properties are not supported: http://node.green/#ESNEXT-candidate--stage-3--object-rest-spread-properties, at least not until Node version 8.60 (at which point the color turns green to indicate that beginning in 8.3, Node doessupport the object spread/rest operator, as pointed out in the other answer)

此外,node.js 兼容性表声称 node.js 完全符合带有数组和可迭代对象的扩展运算符的规范,但特别指出不支持对象 rest/spread 属性:http: //node.green/#ESNEXT-candidate --stage-3--object-rest-spread-properties,至少在 Node 8.60 版本之前不会(此时颜色变为绿色表示从 8.3 开始,Node确实支持对象扩展/rest 运算符,正如所指出的那样在另一个答案中

回答by saadq

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.

对对象使用 rest/spread 是一个单独的建议,您可以在此处阅读。除非达到第 4 阶段,否则年度 ES 版本不会接受提案,目前处于第 3 阶段。它可能会进入 ES2018。如果你现在想使用它,你必须使用像babel.



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

编辑:从 Node v8.3 开始,无需任何转译即可使用对象静止/传播。