Mar 5, 2010

the internal of $.end() method

jQuery methods that alter the original jQuery wrapper set selected are considered to be destructive.The reason being that they do not maintain the original state of the wrapper set. Not to worry;nothing is really destroyed or removed. Rather, the former wrapper set is attached to a new set.

pushStack: function( elems, name, selector ) {
 // Build a new jQuery matched element set
 var ret = jQuery();

 if ( jQuery.isArray( elems ) ) {
  push.apply( ret, elems );
 
 } else {
  jQuery.merge( ret, elems );
 }

 // Add the old object onto the stack (as a reference)
 ret.prevObject = this;

 ret.context = this.context;

 if ( name === "find" ) {
  ret.selector = this.selector + (this.selector ? " " : "") + selector;
 } else if ( name ) {
  ret.selector = this.selector + "." + name + "(" + selector + ")";
 }

 // Return the newly-formed element set
 return ret;
},

 end: function() {
  return this.prevObject || jQuery(null);
 },