我一直在摆弄一些代码 here 。出于某种原因,一个函数被检测为抽象数组 b.c.它有一个长度属性。不是主要问题,BC它是 0,但我发现这很奇怪。

var test_set = [null,
                undefined,
                NaN,
                true,
                false,
                1,
                'a',
                {test:'test'},
                [0],
                function(){},
                /test/
               ];

var index,
    key,
    test;

function isArrayAbstract (obj) {
    return (obj != null) && (obj.length === +obj.length);
};

for(index = 0; index < test_set.length; index++){
    test = isArrayAbstract(test_set[index]);
    console.log('Mark | ' + test_set[index]);
    console.log(test);
}

最佳答案

Function.length



请参阅示例:

console.log( (function () {}).length );  /* 0 */
console.log( (function (a) {}).length ); /* 1 */
console.log( (function (a, b) {}).length ); /* 2 etc. */
console.log( (function (...args) {}).length ); /* 0, rest parameter is not counted */

也引用 ECMAScript Language Specs:

关于javascript - 为什么函数有长度属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17494053/

10-12 12:20
查看更多