According to the docs, the latest node (Node 5+) should support the spread operator by default, like so:
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:
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?
The spread operator can be applied to structures like arrays, string and objects.
One of the alternatives to the spread operator is the Object. assign function. Here is the same function using the object. assign function.
Spread properties is a part of ECMAScript 2018 which is not supported by IE. You can use Babel to transpile it. Thank you for your response.
Note: Spread operator was introduced in ES6. Some browsers may not support the use of spread syntax. Visit JavaScript Spread Operator support to learn more.
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).
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:
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.
Update: As of Node 8.6, object spread syntax is supported.
Update 2: Object spread syntax has finally made its way through the proposal process, and will be part of the ES2018 spec.
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 ...
.
You can use Object.assign
instead:
const newObj = Object.assign({}, oldObj, { newProperty: 1 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With