Prototype 工具函数 学习
作者:bea
$H就是建立Hash对象的便捷方法,关于Hash对象具体参考【Prototype 学习——Hash对象 】 $R就是简历ObjectRange对象的便捷方法,关于ObjectRange对象具体参考【Prototype 学习——ObjectRange对象 】 Try.these: Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw
$H就是建立Hash对象的便捷方法,关于Hash对象具体参考【Prototype 学习——Hash对象 】
$R就是简历ObjectRange对象的便捷方法,关于ObjectRange对象具体参考【Prototype 学习——ObjectRange对象 】
Try.these:
Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.
代码如下:
//就是用一个循环嵌套try...catch完成这个工具函数的
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;
}
};
看一个例子(不同的浏览器有不同的创建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():
根据这个方法的名字大概就能猜到这个方法的用途了。但是这个方法在1.6里面被标记成
deprecated的了。被$$和Eelement.select方法代替了,关于这两个方法,后面在讲。
有用 | 无用
$R就是简历ObjectRange对象的便捷方法,关于ObjectRange对象具体参考【Prototype 学习——ObjectRange对象 】
Try.these:
Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.
代码如下:
//就是用一个循环嵌套try...catch完成这个工具函数的
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;
}
};
看一个例子(不同的浏览器有不同的创建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():
根据这个方法的名字大概就能猜到这个方法的用途了。但是这个方法在1.6里面被标记成
deprecated的了。被$$和Eelement.select方法代替了,关于这两个方法,后面在讲。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- jqPlot jquery的页面图表绘制工具
- IE DOM实现存在的部分问题及解决方法
- 从父页面读取和操作iframe中内容方法
- javaScript 数值型和字符串型之间的转换
- JavaScript中null与undefined分析
- js 字符串操作函数
- javaScript Array(数组)相关方法简述
- 对象特征检测法判断浏览器对javascript对象的支持
- javascript 一个函数对同一元素的多个事件响应
- IE中radio 或checkbox的checked属性初始状态下不能选中显示问题
- javascript 解析后的xml对象的读取方法细解
- jquery 将disabled的元素置为enabled的三种方法
- javaScript 判断字符串是否为数字的简单方法
- 利用javascript/jquery对上传文件格式过滤的方法
- javascript 动态table添加colspan owspan 参数的方法
- jquery 表单下所有元素的隐藏
- 运用jquery实现table单双行不同显示并能单行选中
- 用JQuery 实现AJAX加载XML并解析的脚本
- Prototype Selector对象学习