javascript - apply第二个参数不是应该是个数组吗?对象也行吗?
巴扎黑
巴扎黑 2017-04-11 11:43:01
[JavaScript讨论组]

如图

巴扎黑
巴扎黑

全部回复(2)
天蓬老师

Syntax
fun.apply(thisArg, [argsArray])

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

阿神

折腾了一会儿,现在有个想法
首先,正如熊丸子(抱歉,不知道怎么做链接。。。)所说,{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)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号