Named parameters: Difference between revisions

Line 597:
// => "foo is 0, bar is 3.14, and grill is lamb kebab"</lang>
===ECMAScript 2015 (ES6) variants===
{{incorrect|Javascript| From use inside the function the parameters must be named a, b, and c and are not the names used when calling the function, and so this is not an example of named parameters.}}
With this version, ECMAScript adds destrucuring assignments and destructuring in function parameters. Thus you could do something like this (this works in ES6 Fiddle, but is syntax error in Mozilla SpiderMonkey JS Shell, so uses console.log instead of print):<lang javascript>let
example = // The member name in the object can either be the same as the parameter (as in bar, grill),
example =
// or a different parameter name as in the case of member foo being assigned to parameter a here.
({foo: a=0, bar: b=1, grill: c='pork chops'}={}) => (
console.log('{foo: is ',a,'=0, bar is ',b,'=1, and grill='pork is chops'+c}={})); => (
console.log('foo is ',a,', bar is ',bar,', and grill is '+grill));
 
example();
Anonymous user