栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

Prototype 学习 工具函数学习($A方法)

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Prototype 学习 工具函数学习($A方法)

$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帮助文档的一段说明和例子也许更能说明这个函数的作用:
复制代码 代码如下:

var paras = $A(document.getElementsByTagName('p'));
paras.each(Element.hide);
$(paras.last()).show();

还有一点:
Array.from = $A;
数组对象的from静态方法和$A是一个方法
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/118083.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号