JavaScript :巧用类数组对象生成空数组

来源:互联网 发布:ubuntu 查看磁盘空间 编辑:程序博客网 时间:2024/05/22 15:18

//不使用循环生成 10 个元素都为undefined 的数组var  array_like = {length:10};//或者  Array(10) , new Array(10)var a = Array.apply(null,array_like);// 可以理解为: var  a  =  Array (likeArray[0], likeArray[1]......likeArray[9])var b = [].slice.call(array_like);console.log(a);console.log(b);



参考资料:

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 orundefined 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


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

0 0
原创粉丝点击