Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node 5.10 spread operator not working

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?

like image 683
batjko Avatar asked Apr 16 '16 16:04

batjko


People also ask

Can I use spread operator in node JS?

The spread operator can be applied to structures like arrays, string and objects.

What can I use instead of a spread operator?

One of the alternatives to the spread operator is the Object. assign function. Here is the same function using the object. assign function.

Does IE 11 support spread operator?

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.

Is Spread operator ES6?

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.


2 Answers

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.

like image 137
Joe Clay Avatar answered Sep 30 '22 19:09

Joe Clay


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 }); 
like image 26
Tamas Hegedus Avatar answered Sep 30 '22 18:09

Tamas Hegedus