3

I found this code and I dont understand purpose of comment block in parameters:

if (!Array.prototype.some)
{
  Array.prototype.some = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this && fun.call(thisp, this[i], i, this))
        return true;
    }

    return false;
  };
}

In my opinion it could be normal second parameter and this row could be deleted then:

var thisp = arguments[1];
Luckylooke
  • 4,061
  • 4
  • 36
  • 49

3 Answers3

1

If Array.prototype.some is defined, the original method had a second parameter which is thisp, but since it is not defined, the author defines his/her own method that mimics the functionality.

The author expects that the caller passes the second parameter which is thisp. HTH.

g13n
  • 3,218
  • 2
  • 24
  • 19
1

The implementation of Array.prototype.some built in to Firefox has an arity of 1, i.e. it accepts one argument. In order to implement the second optional argument without changing the arity the replacement code accesses the second argument via arguments[1] instead.

I don't actually know what the EcmaScript specification has to say about the arity of Array.prototype.some.

Neil
  • 54,642
  • 8
  • 60
  • 72
0

you're right, it should be a parameter, because 1. using the functions arguments within a function is expensive and 2. it's hardwired into the function, although it can stay undefined in the actual loop. The method shouldn't be in production code, I'd say. Testing some more, it's not clear what the second parameter actually does anyway. The MDN shim is a strange duck, but maybe I've missed something.

Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177