Default values of destructured arguments?
Already supported: google.github.io/traceur-compiler/demo/repl.html#function foo({d}%3D{d%3A1}) { }, google.github.io/traceur-compiler/demo/repl.html#function foo({d}={d:1}) { }
If you open a console, you can call foo()
.
Traceur supports the former (6to5 doesn't). I think both of those should be valid, the former just because it makes sense (likely why it accidently works in traceur), and the latter because it's very useful.
These should be equivalent (minus the o variable)
function({d=1}){}
function(o){ var d = o instanceof Object && o.d !== undefined ? o.d : 1 }
function foo({d}={d:1}) { }
and
function foo({d=1}) { }
Both are allowed in ES6.
The second will throw an error if no actual parameter is provided to the function, however. (Because you haven't specified a default for the parameter.)
function({d=1}){}
function(o){ var d = o instanceof Object && o.d !== undefined ? o.d : 1 }
Not exactly - see my previous post. You've supplied a default destructuring value, but not a default parameter value.
Odd, traceur seems to be fine with function({d=1}){ return d }() === 1
Try this:
google.github.io/traceur-compiler/demo/repl.html#(function({d%3D1}){ return d }() %3D%3D%3D 1)%0A
Maybe an old version or traceur on jsbin?
Ah yep, it appears so.
I might be missing something but it appears in es6 you cannot default a destructured argument?
and
don't seem to fall into the syntax, are there plans for this in the future or something that prevented this?