@@iterator in arguments object

Allen Wirfs-Brock allen at wirfs-brock.com
Sun Dec 23 18:32:56 PST 2012


On Dec 23, 2012, at 5:35 PM, Brandon Benvie wrote:

> Here's one of the examples that was sticking out in my mind earlier that Brendan's solution takes care of. Array.prototype.reduce requires that if the initial value isn't provided then the first value of the array is the initial value.
> 
> Using rest:
> 
>     function reduce(callback, ...initial){
>       var current, index;
> 
>       if (initial.length) {
>         index = 0;
>         current = initial[0];
>       } else {
>         index = 1;
>         current = this[0];
>       }
>     
>       ...etc...
>     }
> 
> 
> Using arguments.length:
> 
>     function reduce(callback, initial){
>       var current, index;
>       
>       if (arguments.length > 1) {
>         index = 0;
>         current = initial;
>       } else {
>         index = 1;
>         current = this[0];
>       }
>     
>       ...etc...
>     }
> 
> 
> 
> I guess using rest like that is not a bad way to solve it, though it's not saying what you mean but rather kind of hacking around the limitation. Either way, it's certainly possible to forgo using arguments to accomplish the goal.

the way I would express this example  is:

function reduce(callback, ...rest){
  var current, index;
  if (rest.length > 0) {
        index = 0;
        current = rest[0];
      } else {
        index = 1;
        current = this[0];
      }
  ...etc...
}

which seems to exactly express the intent

Allen


More information about the es-discuss mailing list