当前位置: 58彩票app下载 > 58彩票网络 > 正文

工具函数学习

时间:2019-09-21 13:10来源:58彩票网络
$A方法: Accepts an array-like collection (anything with numeric indices) andreturns its equivalent as an actual Array object. This method is aconvenience alias of Array.from, but is the preferred way of casting toan Array. 复制代码 代码

$A方法:
Accepts an array-like collection (anything with numeric indices) and returns its equivalent as an actual Array object. This method is a convenience alias of Array.from, but is the preferred way of casting to an Array.

复制代码 代码如下:

function $A(iterable) {
if (!iterable) return [];
if ('toArray' in Object(iterable)) return iterable.toArray();
var length = iterable.length || 0, results = new Array(length);
while (length--) results[length] = iterable[length];
return results;
}

一旦传进来的参数为null, undefined and false 则平昔再次回到空数组
假设传进来的参数对象里有toArray方法,那直接调用参数的toArray方法,因为有无数Prototype对象里面已经定义好了toArray方法,所以能够一贯调用toArray方法
例如:

复制代码 代码如下:

var array={
toArray : function(){
return [1,2,3];
}
}
//1,2,3
alert($A(array));

接下去依照参数的length营造新数组,然后每一种把参数中的成分复制到新数组中去,最终回来新数组对象
下边给出prototype帮忙文书档案的一段表达和例子恐怕更能表达这几个函数的效应:

复制代码 代码如下:

/*The well-known DOM method document.getElementsByTagName() doesn't return an Array, but a NodeList object that implements the basic array "interface." Internet Explorer does not allow us to extend Enumerable onto NodeList.prototype, so instead we cast the returned NodeList to an Array:*/
var paras = $A(document.getElementsByTagName('p'));
paras.each(Element.hide);
$(paras.last()).show();

再有一点点:
Array.from = $A;
数组对象的from静态方法和$A是一个艺术

编辑:58彩票网络 本文来源:工具函数学习

关键词: