节点5.10扩展操作符不工作

时间:2021-04-24 21:18:00

According to the docs, the latest node (Node 5+) should support the spread operator by default, like so:

根据文档,最新节点(节点5+)默认支持传播算子,如下图所示:

const newObj = {
        ...oldObj,
        newProperty: 1
      }

And I have node 5.10.1 installed (e.g. that's what 'node -v' tells me). But I am still getting this error:

我已经安装了节点5.10.1(例如,“节点-v”就是这样告诉我的)。但我还是犯了这个错误

c:\[myprojectfolder]>node index.js
c:\[myprojectfolder]\index.js:21
            ...oldObj,
            ^^^

SyntaxError: Unexpected token ...
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:146:18)
    at node.js:404:3

What am I missing?

我缺少什么?

3 个解决方案

#1


48  

The array spread syntax is supported, but the object spread syntax is not - this is most likely due to it not being finalized as part of the ECMAScript spec yet (it was originally planned for inclusion in ES7/ES2016, but it got bumped back, if I recall correctly).

支持数组扩展语法,但对象扩展语法不支持——这很可能是因为它还没有作为ECMAScript规范的一部分完成(最初计划将它包含在ES7/ES2016中,但如果我没记错的话,它被回退了)。

Your options in the meantime are either to use a transpiler (such as Babel, with the transform-object-rest-spread plugin), or if that feels like overkill, you can use the new built-in Object.assign function. The object spread syntax is effectively just syntax sugar around Object.assign - the example in your question could be expressed like so:

与此同时,您的选择要么是使用transiler(比如Babel,使用transform-object-rest-spread插件),或者如果这感觉有点过分,您可以使用新的内置对象。分配功能。对象扩展语法实际上就是对象周围的语法糖。赋值——您问题中的示例可以这样表示:

const newObj = Object.assign({}, oldObj, {
    newProperty: 1
});

Note the empty object as the first argument; the properties from the rest of the passed objects get merged into it, with those furthest to the right of the function call taking priority. It may seem more intuitive to simply have oldObj as the first argument, but this doesn't have quite the same effect - it would mutate the existing oldObj as well as returning it. Having an empty object as the target leaves oldObj unchanged.

注意作为第一个参数的空对象;传递的其他对象的属性被合并到其中,函数调用右边最远的属性优先。简单地将oldObj作为第一个参数似乎更直观,但这并没有完全相同的效果——它会在返回之前对现有的oldObj进行突变。将空对象作为目标保留oldObj不变。


Update: As of Node 8.6, object spread syntax is supported.

更新:在节点8.6中,支持对象扩展语法。

#2


3  

What you tried to use is called object spread and is not part of the es2015 specification. Node only supports the regular spread in function calls and array literals. Unfortunately not even array destructuring is supported yet, although they link to the MDN page which describes both structuring and destructuring use of ....

您试图使用的是对象扩展,而不是es2015规范的一部分。节点只支持函数调用和数组文本中的常规扩展。不幸的是不支持数组解构,尽管他们链接MDN页面描述结构化和解构使用....

You can use Object.assign instead:

您可以使用对象。分配:

const newObj = Object.assign({}, oldObj, {
  newProperty: 1
});

#3


1  

That's works in Node.js 8.5.0.

这是在节点工作。js 8.5.0。

Example:

例子:

var oldObj = {
    oldProperty: 0
}

var newObj = {
    ...oldObj,
    newProperty: 1
}

console.log('Old Object: ' + JSON.stringify(oldObj, null, '    '))

console.log('New Object: ' + JSON.stringify(newObj, null, '    '))

Output:

输出:

Old Object: {
    "oldProperty": 0
}
New Object: {
    "oldProperty": 0,
    "newProperty": 1
}

Screenshot from IDE Debug Console:

IDE调试控制台的截图:

节点5.10扩展操作符不工作

#1


48  

The array spread syntax is supported, but the object spread syntax is not - this is most likely due to it not being finalized as part of the ECMAScript spec yet (it was originally planned for inclusion in ES7/ES2016, but it got bumped back, if I recall correctly).

支持数组扩展语法,但对象扩展语法不支持——这很可能是因为它还没有作为ECMAScript规范的一部分完成(最初计划将它包含在ES7/ES2016中,但如果我没记错的话,它被回退了)。

Your options in the meantime are either to use a transpiler (such as Babel, with the transform-object-rest-spread plugin), or if that feels like overkill, you can use the new built-in Object.assign function. The object spread syntax is effectively just syntax sugar around Object.assign - the example in your question could be expressed like so:

与此同时,您的选择要么是使用transiler(比如Babel,使用transform-object-rest-spread插件),或者如果这感觉有点过分,您可以使用新的内置对象。分配功能。对象扩展语法实际上就是对象周围的语法糖。赋值——您问题中的示例可以这样表示:

const newObj = Object.assign({}, oldObj, {
    newProperty: 1
});

Note the empty object as the first argument; the properties from the rest of the passed objects get merged into it, with those furthest to the right of the function call taking priority. It may seem more intuitive to simply have oldObj as the first argument, but this doesn't have quite the same effect - it would mutate the existing oldObj as well as returning it. Having an empty object as the target leaves oldObj unchanged.

注意作为第一个参数的空对象;传递的其他对象的属性被合并到其中,函数调用右边最远的属性优先。简单地将oldObj作为第一个参数似乎更直观,但这并没有完全相同的效果——它会在返回之前对现有的oldObj进行突变。将空对象作为目标保留oldObj不变。


Update: As of Node 8.6, object spread syntax is supported.

更新:在节点8.6中,支持对象扩展语法。

#2


3  

What you tried to use is called object spread and is not part of the es2015 specification. Node only supports the regular spread in function calls and array literals. Unfortunately not even array destructuring is supported yet, although they link to the MDN page which describes both structuring and destructuring use of ....

您试图使用的是对象扩展,而不是es2015规范的一部分。节点只支持函数调用和数组文本中的常规扩展。不幸的是不支持数组解构,尽管他们链接MDN页面描述结构化和解构使用....

You can use Object.assign instead:

您可以使用对象。分配:

const newObj = Object.assign({}, oldObj, {
  newProperty: 1
});

#3


1  

That's works in Node.js 8.5.0.

这是在节点工作。js 8.5.0。

Example:

例子:

var oldObj = {
    oldProperty: 0
}

var newObj = {
    ...oldObj,
    newProperty: 1
}

console.log('Old Object: ' + JSON.stringify(oldObj, null, '    '))

console.log('New Object: ' + JSON.stringify(newObj, null, '    '))

Output:

输出:

Old Object: {
    "oldProperty": 0
}
New Object: {
    "oldProperty": 0,
    "newProperty": 1
}

Screenshot from IDE Debug Console:

IDE调试控制台的截图:

节点5.10扩展操作符不工作