为什么我的传播语法在节点 v7.0.0 中不起作用?

Why isn't my spread syntax working in node v7.0.0?

我正在尝试像这样使用 Object Spread 语法:

let credentialsWithAuth =  { ...credentials, type: 'basic' }

其中 credentials 是一个以用户名和密码作为键和值的对象。但这爆炸了 SyntaxError: Unexpected token ...

所以我必须用 babel 设置节点才能工作吗?我以为现在内置了本机支持。

http://node.green/

我不能在没有 Object.assign 等的情况下使用它吗?

有人可以澄清一下吗?

节点 7.0 中可用的传播语法不处理对象的传播属性。您要找的是 object spread syntax which is currently on stage 3 of TC39 Process. You can find more info about the process in the process document and info about proposal in its repository.

是的,仅在 node_8x 及更高版本中受支持。但是,使用 Object.assign() 的正确等效项(不会覆盖源对象)是:

let credentials = { username : 'test', password: 'test' }
let credentialsWithAuth = Object.assign({}, { type: 'basic' }, credentials)

console.log(credentialsWithAuth)