[jQuery] $(foo)[n]?

[jQuery] $(foo)[n]?

Instead of overloading extend to support both "extend self" and "extend
first argument", I like to have separate functions ala MochiKit. The
advantage is that this allows each function to take multiple arguments.
Here's the code I'm currently using (not jQuery but you'll get the idea):
Z.Object = {
merge: function() {
return Z.Object.updateFromArray( {}, arguments, 0 );
},

copy: function( self ) {
return Z.Object.combine( self )
},

update: function( self ) {
return Z.Object.updateFromArray( self, arguments, 1 );
},

updateFromArray: function( self, array, start, stop ) {
stop = stop || array.length;
for( var i = start; i < stop; i++ ) {
var obj = array[i];
for( var prop in obj ) self[prop] = obj[prop];
}
return self;
}
};
This lets me do things like this (contrived sample code):
var foo = Z.Object.merge( { a:1 }, { b:2 }, { c:3 } );
Z.Object.update( foo, { d:4 }, { e:5 } );
// Now foo is { a:1, b:2, c:3, d:4, e:5 }
Obviously you can do the same things with multiple calls to extend, but I've
found it convenient to combine multiple objects in one step.
-Mike































    • Topic Participants

    • mike