Default values of destructured arguments?

# Bradley Meck (10 years ago)

I might be missing something but it appears in es6 you cannot default a destructured argument?

function foo({d}={d:1}) {

}

and

function foo({d=1}) {

}

don't seem to fall into the syntax, are there plans for this in the future or something that prevented this?

# Axel Rauschmayer (10 years ago)
# Frankie Bagnardi (10 years ago)

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.

jsbin.com/bevijekiki/1/edit

These should be equivalent (minus the o variable)

function({d=1}){}
function(o){ var d = o instanceof Object && o.d !== undefined ? o.d : 1 }
# Kevin Smith (10 years ago)
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.)

# Kevin Smith (10 years ago)
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.

# Frankie Bagnardi (10 years ago)

Odd, traceur seems to be fine with function({d=1}){ return d }() === 1

jsbin.com/birajevaxi/2/edit

# Kevin Smith (10 years ago)
# Frankie Bagnardi (10 years ago)

Ah yep, it appears so.