Skip to content

Commit

Permalink
Remove the need for the internal setArray method, it was only really …
Browse files Browse the repository at this point in the history
…used by pushStack anyway. Fixes #6003.
  • Loading branch information
jeresig committed Feb 13, 2010
1 parent 8404ad6 commit 7a467ed
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/core.js
Expand Up @@ -197,7 +197,14 @@ jQuery.fn = jQuery.prototype = {
// (returning the new matched element set)
pushStack: function( elems, name, selector ) {
// Build a new jQuery matched element set
var ret = jQuery( elems || null );
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;
Expand All @@ -214,18 +221,6 @@ jQuery.fn = jQuery.prototype = {
return ret;
},

// Force the current matched set of elements to become
// the specified array of elements (destroying the stack in the process)
// You should use pushStack() in order to do this, but maintain the stack
setArray: function( elems ) {
// Resetting the length to 0, then using the native Array push
// is a super-fast way to populate an object with array-like properties
this.length = 0;
push.apply( this, elems );

return this;
},

// Execute a callback for every element in the matched set.
// (You can seed the arguments with an array of args, but this is
// only used internally.)
Expand Down

2 comments on commit 7a467ed

@sunliangqin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 200 - 207: why not use jQuery.merge(ret, elems) directly?

@jeresig
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's faster to use push.apply if the incoming set is already an array.

Please sign in to comment.