[jQuery] Binding events to newly added DOM elements
I like your version better. It's probably faster too.
While playing with this code, I noticed that this.size() goes to more work
than it needs to. Even if you want to document size() as the proper external
interface, it seems that internal methods could use this.cur.length without
violating any design principles. Or at least change size() to this (or do
both):
size: function() { return this.cur.length; },
What the heck, while I'm at it I would change each() from:
each: function(f) {
for ( var i = 0; i < this.size(); i++ )
$.apply( this.get(i), f, [i] );
return this;
},
To:
each: function(f) {
for ( var i = 0; i < this.cur.length; i++ )
$.apply( this.cur[i], f, [i] );
return this;
},
That removes two function calls from each iteration and it seems just as
easy to read and maintain.
Although I suppose if you were *really* optimizing it you would do this:
each: function(f) {
var cur = this.cur;
var length = cur.length;
for ( var i = 0; i < length; i++ )
$.apply( cur[i], f, [i] );
return this;
},
-Mike
> From: John Resig
>
> I'm not sure if it has to be that complex, probably something like:
>
> append: function() {
> var clone = this.size() > 1;
> var a = $.clean(arguments);
> return this.each(function(){
> for ( var i in a )
> this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
> });
> },
>
> would be sufficient. Although, breaking it off into a
> separate function would definitely save on writing the same
> stuff over and over.
>
> --John
>
> On 3/8/06, Michael Geary <Mike@geary.com>