开发者

length property of functions

开发者 https://www.devze.com 2023-03-17 08:41 出处:网络
I don\'t understand what\'s going on here: $ node > var f = function() {}; > f[\'length\'] = \'11\';

I don't understand what's going on here:

$ node
> var f = function() {};
> f['length'] = '11';
'11'
> f['length']
0

If you're not familiar with node, the code after > is what I typed, and the stuff not there are the returned values. 开发者_如何转开发So f['length'] == 0.


The function object already have a property named length, and you can't change it.

The length property is the number of parameters there is in the function definition.

Example:

> var f = function(x) {};
> f.length
1


In Javascript, all functions have a length property. It is the number of parameters that the function is defined with, and the property is read only.

Because you define no parameters (function() {}) the length property will always be 0.


Length property for functions is the number of arguments a function expects.

Specifies the number of arguments expected by the function.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Length

In your case, f doesn't take any paramater, so its length is 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消