如果我坚持使用Prototype而不是jQuery,但我喜欢jQuery的链接能力,那么通过以下方式为CSS选择器带来这个功能有什么缺点吗?
function r(f) {
return function() {
var args = [];
for(var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
this.each(function(e) {
if (e && (typeof e[f] == 'function' || typeof e[f] == 'object'))
e[f].apply(e, args);
});
return this;
}
}
function om(p1, p2) {
for( prop in p1 ) {
if( p1.hasOwnProperty( prop ) ){
p2[prop] = r(prop);
}
}
}
om(Element.Methods, Array.prototype);
潜伏着一些改进/增强。但是代码中是否有真的错误或我完全错过了什么?
由于使用了这段代码,现在我可以执行以下操作...
$$('.something').html('blah').show();
答案 0 :(得分:1)
在Prototype.js中执行“链接”的标准方法是使用“each”显式迭代选择器返回的元素数组。例如:
$$(".something").each(function(e){e.update("foo").show();});
话虽如此,我认为您的代码没有任何问题,只要它符合您的编码风格!