The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Default Values for Function Parameters in ES6

Default Values for Function Parameters in ES6

With ECMAscript 6 we get the luxury of default parameters for our functions.
Watch this lesson now

With ECMAscript 6 we get the luxury of default parameters for our functions.

Roman Ganchenko

Is there any specific reason why you are NOT recommending using arrow function new syntax?

In reply to

There's no general reason not to use arrow functions. But arrow functions are anonymous functions, so everything that applies to anonymous functions also applies to arrow functions.

You should generally avoid anonymous functions except for very trivial code. Anonymous functions leave less helpful stack traces and it's more difficult to understand their intent.

Additionally arrow functions are always bound to the current this, so they may not actually do what you want. For example, when defining an ES5-style "class" (i.e. constructor function with a prototype property), you explicitly want this to be unbound, so arrow functions won't give you what you want.

In my opinion, arrow functions are useful where-ever you would normally use anonymous functions and either don't care about this or would otherwise use Function.prototype.bind or a local variable.

In reply to Roman Ganchenko

I run the code , but there is SyntaxError:

/usr/local/Cellar/node/5.6.0/bin/node --use-strict /Users/huanghaitao/WebstormProjects/es6/default.js
let receive = (complete = () => console.log("complete")) => complete()

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:140:18)
at node.js:1001:3

Process finished with exit code 1

the config not right?


you don't have es6 enabled.

In reply to Haitao.Huang
Joel's Head
Why are we asking?