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

(编辑:jimmy 日期: 2024/10/10 浏览:2)

$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是一个方法
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。