Iterating default function arguments
Hi there, I was wondering if there were any plans to modify `arguments` to include default parameters (e.g. changing it from a simpleParameterList) or to include a new property that does allow iteration of all values available to a function. for example: function foo( a, b = 2 ) { return arguments; } console.log( foo( 1 ) ); // outputs [ 1 ], not [ 1, 2 ] Currently I can't see a way of getting default parameters as an iterable object. I filed a bug with Mozilla over this, but they pointed out that the behaviour matches the spec. https://bugzilla.mozilla.org/show_bug.cgi?id=1144672 Regards, Robin On 25 March 2015 at 05:39, es-discuss-requ...@mozilla.org wrote: Send es-discuss mailing list submissions to es-discuss@mozilla.org To subscribe or unsubscribe via the World Wide Web, visit https://mail.mozilla.org/listinfo/es-discuss or, via email, send a message with subject or body 'help' to es-discuss-requ...@mozilla.org You can reach the person managing the list at es-discuss-ow...@mozilla.org When replying, please edit your Subject line so it is more specific than Re: Contents of es-discuss digest... Today's Topics: 1. Re: Object arithmetic--operator alternative to Object.assign (Edwin Reynoso) 2. Extended object literal property value shorthand (Bob Myers) 3. Re: Extended object literal property value shorthand (Bob Myers) -- Forwarded message -- From: Edwin Reynoso eor...@gmail.com To: Rick Waldron waldron.r...@gmail.com Cc: Bob Myers r...@gol.com, es-discuss es-discuss@mozilla.org Date: Tue, 24 Mar 2015 23:09:30 -0400 Subject: Re: Object arithmetic--operator alternative to Object.assign Yea, um I'm only 16 and been programming in Javascript for about 2-3 years. I haven't gotten to the Nitty Gritty part of Javascript specs. So I won't be able to help out with that. I was just throwing out what I mentioned above. On Tue, Mar 24, 2015 at 10:39 PM, Rick Waldron waldron.r...@gmail.com wrote: On Tue, Mar 24, 2015 at 7:09 PM Edwin Reynoso eor...@gmail.com wrote: For different objects this is the only way I see possible with destructuring. IMO it's a bit ugly and weird to read deep destructuring: ``` let x = { a: 1 }; let y = { b: 2 }; let { x: { a }, y: { b } } = { x, y }; ``` But I'd prefer Bob Myers's way: ``` let x = { a: 1 }; let y = { b: 2 }; {x.a, y.b} ``` Now that would be for destructuring. But isn't the following shorthand property assignment not destructuring: ``` var c = {x,y}; //so I'm thinking Bob wants the following: var c = {x.a, b.y}; // {a: 1, b: 2} ``` As an exercise to see if this is reasonable, I spent some time drafting an outline addition to 12.2.5.9 Runtime Semantics: PropertyDefinitionEvaluation that handled a newly defined (ie. thing I made up) PropertyDefinition : IdentifierNameReference, but ran into issues when I had to consider all the forms that MemberExpression includes. https://people.mozilla.org/~jorendorff/es6-draft.html#sec-left-hand-side-expressions Rick On Tue, Mar 24, 2015 at 3:51 PM, Tab Atkins Jr. jackalm...@gmail.com wrote: On Tue, Mar 24, 2015 at 9:44 AM, Bob Myers r...@gol.com wrote: Apologies if something like this has already been proposed. We have simplified object literal syntax: {a, b} However, I often find myself writing this: {a: x.a, b: y.b} Would it be possible to have a syntax such as {x.a, y.b} Where the property name is taken from the last segment of the property reference, so that `x.a` becomes the value of property `a`? If you're taking both values from the *same* object, we have the syntax: {a, b} = x; This may or may not help you. ~TJ ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss -- Forwarded message -- From: Bob Myers r...@gol.com To: Rick Waldron waldron.r...@gmail.com, es-discuss es-discuss@mozilla.org Cc: Date: Wed, 25 Mar 2015 09:08:16 +0530 Subject: Extended object literal property value shorthand Thanks Rick. Yes, I was hoping to make the following work: x = {a: 1}; y = {b: 2}; z = {x.a, b.y}; // {a: 1, b: 2} This is not destructuring, it's an extension to object literal property value shorthand. The idea is to derive the desired property name `a` from `x.a`. However, it could be difficult to define when and how a property name could be derived from various types of member expressions. Here is an alternative, admittedly not too pretty, but perhaps easier to define and/or implement. The idea is to generalize the permissible properties for which values can be omitted/inferred, and also allow LHS destructuring-type syntax, so: x = {a: 1}; y = {b:
Re: Iterating default function arguments
On Wed, Mar 25, 2015 at 2:40 AM Robin Cafolla ro...@zombiemongoose.com wrote: Hi there, I was wondering if there were any plans to modify `arguments` to include default parameters (e.g. changing it from a simpleParameterList) or to include a new property that does allow iteration of all values available to a function. for example: function foo( a, b = 2 ) { return arguments; } console.log( foo( 1 ) ); // outputs [ 1 ], not [ 1, 2 ] Currently I can't see a way of getting default parameters as an iterable object. I filed a bug with Mozilla over this, but they pointed out that the behaviour matches the spec. https://bugzilla.mozilla.org/show_bug.cgi?id=1144672 This is correct, because only one _argument_ was passed, therefore the arguments object has only one entry. Parameters are not the same as Arguments. Therefore: foo(1, 3) = [1, 3] Because two arguments were passed. And: foo(1, 2, 3) = [1, 2, 3] Because three were passed. Hopefully that helps clarify? Rick ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Iterating default function arguments
I think the main concern was: Currently I can't see a way of getting default parameters as an iterable object. and indeed `arguments` is not the magic variable you are looking for. However, you are inside your own defined function so either you don't specify defaults, or you iterate over `[a, b]` ? Interesting enough, there's no actually a way to understand the function signature, in terms of optionally accepted arguments. `arguments.length` is one and same is for `foo.length` so if we'd like to understand if anyone invoked the function passing or not all optional parameters, how could we proceed? Lets say `foo` would like to delegate its own arguments including defined defaults to another function ... how can we grab these at runtime? I think the answer is still a static `[a, b]` as list of arguments to apply, but I can see some lack of reflection capability ... or maybe Reflect would solve this? Regards On Wed, Mar 25, 2015 at 4:56 PM, Rick Waldron waldron.r...@gmail.com wrote: On Wed, Mar 25, 2015 at 2:40 AM Robin Cafolla ro...@zombiemongoose.com wrote: Hi there, I was wondering if there were any plans to modify `arguments` to include default parameters (e.g. changing it from a simpleParameterList) or to include a new property that does allow iteration of all values available to a function. for example: function foo( a, b = 2 ) { return arguments; } console.log( foo( 1 ) ); // outputs [ 1 ], not [ 1, 2 ] Currently I can't see a way of getting default parameters as an iterable object. I filed a bug with Mozilla over this, but they pointed out that the behaviour matches the spec. https://bugzilla.mozilla.org/show_bug.cgi?id=1144672 This is correct, because only one _argument_ was passed, therefore the arguments object has only one entry. Parameters are not the same as Arguments. Therefore: foo(1, 3) = [1, 3] Because two arguments were passed. And: foo(1, 2, 3) = [1, 2, 3] Because three were passed. Hopefully that helps clarify? Rick ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Iterating default function arguments
I'm all for using Reflect, but looking at the API as listed on MDN, the harmony-reflect github, or the spec, i don't see an obvious way of getting the parameters. If I understand you correctly (and this is not about parsing the parameter definitions) then how about the following solution? ```js function foo(…args) { let [a, b = 2] = args; return args; } ``` Original code: ```js function foo( a, b = 2 ) { return arguments; } ``` -- Dr. Axel Rauschmayer a...@rauschma.de rauschma.de ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Iterating default function arguments
Well my use case is for something I can insert into an existing function, in fact, a lot of functions, hopefully without manually listing the arguments. ```js function bar( parameters ) { for ( var i = 0; i parameters.length; i++ ) { // do something interesting with each parameter of the function } } function foo( a, b = 2 ) { bar( parameters ); // do normal foo stuff } ``` Thinking about it I can't see how Reflect could do this, because it's surely meant to be external to the function. For example with an imaginary Reflect.getParameters: ```js Reflect.getParameters( foo ) // could only give me [ a: undefined, b: 2 ] as reflect can't know what foo was called with from outside the function. ``` On 25 March 2015 at 19:12, Axel Rauschmayer a...@rauschma.de wrote: I'm all for using Reflect, but looking at the API as listed on MDN, the harmony-reflect github, or the spec, i don't see an obvious way of getting the parameters. If I understand you correctly (and this is not about parsing the parameter definitions) then how about the following solution? ```js function foo(…args) { let [a, b = 2] = args; return args; } ``` Original code: ```js function foo( a, b = 2 ) { return arguments; } ``` -- Dr. Axel Rauschmayer a...@rauschma.de rauschma.de ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Iterating default function arguments
Andrea Giammarchi wrote: I think the answer is still a static `[a, b]` as list of arguments to apply, but I can see some lack of reflection capability ... or maybe Reflect would solve this? Yes, Reflect -- we want a mirror approach, not more magic object like arguments or function. /be ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Iterating default function arguments
Sounds good to me and makes perfect sense. Robin? On Wed, Mar 25, 2015 at 5:23 PM, Brendan Eich bren...@mozilla.org wrote: Andrea Giammarchi wrote: I think the answer is still a static `[a, b]` as list of arguments to apply, but I can see some lack of reflection capability ... or maybe Reflect would solve this? Yes, Reflect -- we want a mirror approach, not more magic object like arguments or function. /be ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Iterating default function arguments
Oops. Sorry about that. On 25 March 2015 at 19:01, Andrea Giammarchi andrea.giammar...@gmail.com wrote: you keep replying to me only ... you really need to reply-all since I've actually no idea ;-) On Wed, Mar 25, 2015 at 5:53 PM, Robin Cafolla ro...@zombiemongoose.com wrote: I'm all for using Reflect, but looking at the API as listed on MDN, the harmony-reflect github, or the spec, i don't see an obvious way of getting the parameters. https://github.com/tvcutsem/harmony-reflect/blob/master/doc/api.md What am I missing? On 25 March 2015 at 18:39, Andrea Giammarchi andrea.giammar...@gmail.com wrote: Sounds good to me and makes perfect sense. Robin? On Wed, Mar 25, 2015 at 5:23 PM, Brendan Eich bren...@mozilla.org wrote: Andrea Giammarchi wrote: I think the answer is still a static `[a, b]` as list of arguments to apply, but I can see some lack of reflection capability ... or maybe Reflect would solve this? Yes, Reflect -- we want a mirror approach, not more magic object like arguments or function. /be ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss