Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is function.apply.length 2 in JavaScript?

When I run this code on console:

(function test(){
   console.log(function(){}.apply.length);
})();

The output is: 2

I read about Function length. I understand the function length section on MDN. But I don't understand this. There are no parameters in function. Why is the output 2?

like image 931
Tamerlan Avatar asked Oct 24 '25 12:10

Tamerlan


2 Answers

It's because you're asking for the .length of Function.prototype.apply, which is 2 for its thisArg and its argArray.

19.2.3.1Function.prototype.apply ( thisArg, argArray ) <-- two parameters

Because of apply which accepts 2 parameters. Without apply it will show 0

(function test() {
  console.log(function() {}.length)
})();
like image 31
brk Avatar answered Oct 26 '25 00:10

brk



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!