I'm reading about the special cases of funciones flecha (arrow functions
) in javascript and came across an example that I couldn't run.
The example is:
[[1,2], [3,4]].map(([a,b]) => a + b);
which if we try to print it to the console should give us this result:
console.log([[1,2], [3,4]].map(([a,b]) => a + b));
//[ 3, 7 ] <== resultado
The problem is that when trying to execute that line, I get the following error:
console.log([[1,2], [3,4]].map(([a,b]) => a + b));
^
SyntaxError: Unexpected token [
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:430:10)
at startup (node.js:141:18)
at node.js:1003:3
I am using strict mode:
'use strict';
I have done the tests with these versions of node (on OSX):
5.4.0
5.0.0
4.3.0
4.0.0
In all cases, the result has been the same, why?
The reason it may not work on some platforms or browsers is because the ECMAScript 2015 Destructuring assignment feature that allows the "unset array" syntax is still under development on some platforms:
Node.js
In Node.js Destructuring assignment is experimental and disabled by default.
In order to enable it, run Node with the flag
--harmony-destructuring
Example:
Chrome
In Chrome Destructuring is activated by default since version 49 , so the example should run without problems with the latest version of Chrome.
In previous versions:
chrome://flags/#enable-javascript-harmony
Edge
Edge implements it as experimental features. In order to activate support:
about:flags