Prototype 工具函數(shù) 學(xué)習(xí)
更新時間:2009年07月23日 23:24:00 作者:
Prototype學(xué)習(xí)工具函數(shù)$H,$R,Try.these,document.getElementsByClassName
$H就是建立Hash對象的便捷方法,關(guān)于Hash對象具體參考【Prototype 學(xué)習(xí)——Hash對象 】
$R就是簡歷ObjectRange對象的便捷方法,關(guān)于ObjectRange對象具體參考【Prototype 學(xué)習(xí)——ObjectRange對象 】
Try.these:
Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.
//就是用一個循環(huán)嵌套try...catch完成這個工具函數(shù)的
var Try = {
these: function() {
var returnValue;
for (var i = 0, length = arguments.length; i < length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch (e) { }
}
return returnValue;
}
};
看一個例子(不同的瀏覽器有不同的創(chuàng)建XMLHttpRequest的方法):
getTransport: function() {
return Try.these(
function() { return new XMLHttpRequest() },
function() { return new ActiveXObject('Msxml2.XMLHTTP') },
function() { return new ActiveXObject('Microsoft.XMLHTTP')
} ) || false; }
document.getElementsByClassName():
根據(jù)這個方法的名字大概就能猜到這個方法的用途了。但是這個方法在1.6里面被標(biāo)記成
deprecated的了。被$$和Eelement.select方法代替了,關(guān)于這兩個方法,后面在講。
$R就是簡歷ObjectRange對象的便捷方法,關(guān)于ObjectRange對象具體參考【Prototype 學(xué)習(xí)——ObjectRange對象 】
Try.these:
Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.
復(fù)制代碼 代碼如下:
//就是用一個循環(huán)嵌套try...catch完成這個工具函數(shù)的
var Try = {
these: function() {
var returnValue;
for (var i = 0, length = arguments.length; i < length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch (e) { }
}
return returnValue;
}
};
看一個例子(不同的瀏覽器有不同的創(chuàng)建XMLHttpRequest的方法):
復(fù)制代碼 代碼如下:
getTransport: function() {
return Try.these(
function() { return new XMLHttpRequest() },
function() { return new ActiveXObject('Msxml2.XMLHTTP') },
function() { return new ActiveXObject('Microsoft.XMLHTTP')
} ) || false; }
document.getElementsByClassName():
根據(jù)這個方法的名字大概就能猜到這個方法的用途了。但是這個方法在1.6里面被標(biāo)記成
deprecated的了。被$$和Eelement.select方法代替了,關(guān)于這兩個方法,后面在講。
相關(guān)文章
Prototype RegExp對象 學(xué)習(xí)
幫助文檔上沒有這個對象,實(shí)際上源代碼中這個對象還是有方法的,就1靜態(tài)方法,作用也不是很大,這里簡單說一下,因?yàn)橐院蠼榻B別的對象時會用到這個RegExp2009-07-07
滾動經(jīng)典最新話題[prototype框架]下編寫
滾動經(jīng)典最新話題[prototype框架]下編寫...2006-10-10
Prototype的Class.create函數(shù)解析
Prototype中的類的創(chuàng)建,一般使用Class.create方法來創(chuàng)建,例如PeriodicalExecuter類型。使用的時候通過調(diào)用new PeriodicalExecuter(xxx)來生成對象。2011-09-09
基礎(chǔ)的prototype.js常用函數(shù)及其用法
基礎(chǔ)的prototype.js常用函數(shù)及其用法...2007-03-03

