> > From: Michael Geary
> >
> > Armed with this knowledge, one might be tempted to load
> > all the Array methods in one fell swoop:
> >
> >    jQuery.fn.prototype = Array.prototype;
> >
> > But that does not work, presumably because jQuery is 
> > already being a bit sneaky about its Array-like behavior.

> From: Jörn Zaefferer
> You try to assign the Array prototype to the prototype of the 
> jQuery prototype, right?
> After some experiments on the firebug console, I think the 
> reason for this no working is the Array prototype: It is just 
> an empty error, see for yourself:
> console.debug( Array.prototype );
> Therefore jQuery.fn.extend(Array.prototype) doesn't work either.

I'm not sure why it doesn't work, but that's not the reason. Array.prototype
is populated with the Array methods, but like most or all of the native
objects, those methods are marked as non-enumerable. Try these in the
FireBug console:

   console.debug( Array.prototype.sort );

   console.debug( Array.prototype.reverse );

In fact, instead of:

   jQuery.fn.reverse = [].reverse;

This would work the same:

   jQuery.fn.reverse = Array.prototype.reverse;

And you can confirm that they are identical in the FireBug console:

   [].reverse === Array.prototype.reverse

> But we could just add something like this:
> var arr = "reverse,pop,push,join,shift,rest of the array 
> methods".split(","); for(var i=0; i<arr.length; i++) {
>     jQuery.fn[arr[i]] = [][arr[i]];
> }

That would do the trick. BTW, just for fun, here is another way I like to
write these kinds of loops when using a small number of items:

   var names = { reverse:1, sort:1, etc. };
   for( var name in names )
      jQuery.fn[name] = [][name];


jQuery mailing list

Reply via email to