Reputation: 9638
I am coming from Python and am really liking the way to set named parameters and default values—now it seems that ES6 allows me to do similar. But I can't see why this last call breaks:
fun = ({first=1, last=1}) => (1*first+2*last)
console.log("-----------")
console.log( fun({first:1, last:2}) )
console.log("-----------")
console.log( fun({last:1, first:2}) )
console.log("-----------")
console.log( fun() ) // Breaks
Upvotes: 8
Views: 15907
Reputation: 138234
Cause you need an object which can be deconstructed:
fun({})
Upvotes: 3
Reputation: 386512
You need a default object.
var fun = ({ first = 1, last = 1 } = {}) => 1 * first + 2 * last;
// ^^^^
console.log(fun({ first: 1, last: 2 }));
console.log(fun({ last: 1, first: 2 }));
console.log(fun());
Upvotes: 35