If I’m stuck with Prototype instead of jQuery, yet I like the chain-ability of jQuery, is there some downside to bringing that functionality for the CSS selector via the following?
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);
There are a couple of improvements/enhancements lurking. But is there something really wrong with the code or am I missing something completely?
As a result of using this piece of code, now I can do the following…
$$('.something').html('blah').show();
An example: http://jsbin.com/olojo5/10/edit
The standard way to do “chaining” in Prototype.js is to explicitly iterate on the array of elements returned by a selector using “each”. For example:
That being said, I see nothing wrong with your code, as long as it fits your coding style!