Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($A方法)
更新時(shí)間:2009年07月12日 22:09:04 作者:
Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($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;
}
如果傳進(jìn)來的參數(shù)為null, undefined and false 則直接返回空數(shù)組
如果傳進(jìn)來的參數(shù)對(duì)象里有toArray方法,這直接調(diào)用參數(shù)的toArray方法,因?yàn)橛性S多Prototype對(duì)象里面已經(jīng)定義好了toArray方法,所以可以直接調(diào)用toArray方法
例如:
var array={
toArray : function(){
return [1,2,3];
}
}
//1,2,3
alert($A(array));
接下來根據(jù)參數(shù)的length建立新數(shù)組,然后逐個(gè)把參數(shù)中的元素復(fù)制到新數(shù)組中去,最后返回新數(shù)組對(duì)象
下面給出prototype幫助文檔的一段說明和例子也許更能說明這個(gè)函數(shù)的作用:
/*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();
還有一點(diǎn):
Array.from = $A;
數(shù)組對(duì)象的from靜態(tài)方法和$A是一個(gè)方法
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.
復(fù)制代碼 代碼如下:
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;
}
如果傳進(jìn)來的參數(shù)為null, undefined and false 則直接返回空數(shù)組
如果傳進(jìn)來的參數(shù)對(duì)象里有toArray方法,這直接調(diào)用參數(shù)的toArray方法,因?yàn)橛性S多Prototype對(duì)象里面已經(jīng)定義好了toArray方法,所以可以直接調(diào)用toArray方法
例如:
復(fù)制代碼 代碼如下:
var array={
toArray : function(){
return [1,2,3];
}
}
//1,2,3
alert($A(array));
接下來根據(jù)參數(shù)的length建立新數(shù)組,然后逐個(gè)把參數(shù)中的元素復(fù)制到新數(shù)組中去,最后返回新數(shù)組對(duì)象
下面給出prototype幫助文檔的一段說明和例子也許更能說明這個(gè)函數(shù)的作用:
復(fù)制代碼 代碼如下:
/*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();
還有一點(diǎn):
Array.from = $A;
數(shù)組對(duì)象的from靜態(tài)方法和$A是一個(gè)方法
相關(guān)文章
基礎(chǔ)的prototype.js常用函數(shù)及其用法
基礎(chǔ)的prototype.js常用函數(shù)及其用法...2007-03-03Prototype ObjectRange對(duì)象學(xué)習(xí)
ObjectRange對(duì)象基本就是實(shí)現(xiàn)了連續(xù)的數(shù)字或者字符串,其中只包含一個(gè)方法,include,判斷某個(gè)數(shù)字或者字符串是否在ObjectRange里。并且ObjectRange對(duì)象還混入了Enumerable的方法,所以可以直接在ObjectRange對(duì)象上調(diào)用Enumerable對(duì)象里面的方法。2009-07-07prototype 1.5 & scriptaculous 1.6.1 學(xué)習(xí)筆記
prototype 1.5 & scriptaculous 1.6.1 學(xué)習(xí)筆記...2006-09-09JavaScript使用prototype定義對(duì)象類型
JavaScript使用prototype定義對(duì)象類型...2007-02-02Prototype Date對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象里面就一個(gè)toJSON方法,非常簡單2009-07-07