【发布时间】:2011-07-02 23:19:03
【问题描述】:
在每个表单域上触发事件,我使用原型函数启动一个数据控件。如果它在一个数组内容listdatatypes中找到当前对象id obj.id的字段类型,那么它会进一步处理一些正则表达式控件(当然,还有一个php 覆盖层,但没有 Ajax,因为我想避免重新编码所有内容)。
这就像一个魅力,但我想知道一旦找到针头,如何中断数组针头搜索的传播(例如所谓的 prototype 2)。
代码原理如下:
// proto 1
if (!String.prototype.Contains) {
String.prototype.Contains = function(stack) {
return this.indexOf(stack) != -1;
};
}
// proto 2
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(my_callback)
{
var len = this.length;
if (typeof my_callback != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
my_callback.call(thisp, this[i], i, this);
}
};
}
// ... main code abstract
function str_search(element, index, array){
// Store the found item ... and would like to stop the search propagation
if (element.Contains(obj.id) )
stored_id = obj.id;
}
listdatatypes.forEach(str_search) ;
// ...
谢谢
【问题讨论】:
标签: javascript execution interrupt prototype-programming