Iterating default function arguments

Robin Cafolla robin at zombiemongoose.com
Wed Mar 25 17:25:00 UTC 2015


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 <axel at 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
> axel at rauschma.de
> rauschma.de
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150325/e271ea3d/attachment.html>


More information about the es-discuss mailing list