Prototype 学习——工具函数学习($A方法)
2009-07-11 20:13
537 查看
$A方法:
Accepts an array-like collection (anything with numeric indices) and returns its equivalent as an actual
如果传进来的参数为null,
如果传进来的参数对象里有toArray方法,这直接调用参数的toArray方法,因为有许多Prototype对象里面已经定义好了toArray方法,所以可以直接调用toArray方法
例如:
接下来根据参数的length建立新数组,然后逐个把参数中的元素复制到新数组中去,最后返回新数组对象
下面给出prototype帮助文档的一段说明和例子也许更能说明这个函数的作用:
还有一点:
Array.from = $A;
数组对象的from静态方法和$A是一个方法
Accepts an array-like collection (anything with numeric indices) and returns its equivalent as an actual
Arrayobject. 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,
undefinedand
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是一个方法
相关文章推荐
- Prototype 学习 工具函数学习($A方法)
- Prototype 学习 工具函数学习($A方法)
- Prototype 学习——工具函数学习($方法)
- Prototype 学习 工具函数学习($方法)
- Prototype 学习 工具函数学习($方法)
- Prototype 学习——工具函数学习($w,$F方法)
- Prototype 学习 工具函数学习($w,$F方法)
- map方法学习,封装函数--实现Array.prototype.map()的功能
- Ajax学习笔记之Prototype的工具函数汇总
- Prototype 工具函数 学习
- Prototype学习——工具函数($H,$R,Try.these,document.getElementsByClassName)
- 深入学习PHP数组函数使用方法
- NSPathUtilities.h常用路径工具、函数和方法
- 从零开始学习jQuery (九) jQuery工具函数
- (05)Java学习笔记——函数(方法)
- three.js学习 函数使用方法散记2
- three.js学习 函数使用方法散记3
- Scala学习之——变量、函数、方法及两者联系