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)來(lái)的參數(shù)為null, undefined and false 則直接返回空數(shù)組
如果傳進(jìn)來(lái)的參數(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));
接下來(lái)根據(jù)參數(shù)的length建立新數(shù)組,然后逐個(gè)把參數(shù)中的元素復(fù)制到新數(shù)組中去,最后返回新數(shù)組對(duì)象
下面給出prototype幫助文檔的一段說(shuō)明和例子也許更能說(shuō)明這個(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)來(lái)的參數(shù)為null, undefined and false 則直接返回空數(shù)組
如果傳進(jìn)來(lái)的參數(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));
接下來(lái)根據(jù)參數(shù)的length建立新數(shù)組,然后逐個(gè)把參數(shù)中的元素復(fù)制到新數(shù)組中去,最后返回新數(shù)組對(duì)象
下面給出prototype幫助文檔的一段說(shuō)明和例子也許更能說(shuō)明這個(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)文章
Prototype Class對(duì)象學(xué)習(xí)
Prototype’s object for class-based OOP.prototype OOP編程的基礎(chǔ),詳細(xì)說(shuō)明一下源碼2009-07-07
Prototype 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-07
Prototype源碼淺析 String部分(四)之補(bǔ)充
Prototype源碼淺析 String部分(四)之補(bǔ)充,需要的朋友可以參考下。2012-01-01
滾動(dòng)經(jīng)典最新話題[prototype框架]下編寫
滾動(dòng)經(jīng)典最新話題[prototype框架]下編寫...2006-10-10
Prototype Date對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象里面就一個(gè)toJSON方法,非常簡(jiǎn)單2009-07-07
prototype 源碼中文說(shuō)明之 prototype.js
prototype 源碼中文說(shuō)明之 prototype.js...2006-09-09
Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($方法)
$方法——被成為瑞士軍刀$方法使用技巧。2009-07-07
Prototype RegExp對(duì)象 學(xué)習(xí)
幫助文檔上沒(méi)有這個(gè)對(duì)象,實(shí)際上源代碼中這個(gè)對(duì)象還是有方法的,就1靜態(tài)方法,作用也不是很大,這里簡(jiǎn)單說(shuō)一下,因?yàn)橐院蠼榻B別的對(duì)象時(shí)會(huì)用到這個(gè)RegExp2009-07-07

