@@iterator in arguments object

Brandon Benvie brandon at brandonbenvie.com
Sun Dec 23 17:35:13 PST 2012


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121223/a6fb7096/attachment.html>


More information about the es-discuss mailing list