Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is second function parameter in comment block? just curious

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];
like image 522
Luckylooke Avatar asked Dec 07 '25 03:12

Luckylooke


2 Answers

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.

like image 134
Neil Avatar answered Dec 08 '25 15:12

Neil


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.

like image 42
g13n Avatar answered Dec 08 '25 17:12

g13n



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!