如图
这个对象是类数组对象
.
可能在apply在传递参数的时候会主动调用一次转换,将第二个参数转化为数组吧,这时候类数组对象就刚好转成数组了。
写个简单的例子:
var obj = {test: 1};
Array.apply(null, obj);
var obj2 = [].slice.call(obj);
Array.apply(null, obj2);
它们的结果是相同的,说明apply函数对第二个参数做过一次转换
折腾了一会儿,现在有个想法
首先,正如熊丸子(抱歉,不知道怎么做链接。。。)所说,{length: 5}
是一个类数组对象,它类似于[undefined,undefined,undefined,undefined,undefined]
而Array方法
就是Array对象
的构造器,[].constructor == Array
返回true
于是Array.apply(null, {length:5})
就可以等效于Array.apply(null, [undefined, undefined, undefined, undefined, undefined])
也即是Array(undefined, undefined, undefined, undefined, undefined)
或者new Array(undefined, undefined, undefined, undefined, undefined)
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
4 回答2.4k 阅读✓ 已解决
3 回答1.4k 阅读✓ 已解决
Parameters
thisArg
The value of this provided for the call to fun. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed.
argsArray
An array-like object, specifying the arguments with which fun should be called, or null or undefined if no arguments should be provided to the function. Starting with ECMAScript 5 these arguments can be a generic array-like object instead of an array. See below for browser compatibility information.
从es5开始支持array-like的object啦,所有array-like就是有length属性,有index作为key的object啦。
你这个object正巧就有length属性,不过没有键值对,所以遍历出5个undefined