var test = $();
test.push( $('div')[0] );
This works. Any idea why?
Since push is a array method, I don’t understand why this works in this case, any idea?
Is there a practical use for it since you can use .add() ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They borrow
.push()fromArray.prototype.https://github.com/jquery/jquery/blob/1.6.2/src/core.js#L70
and
https://github.com/jquery/jquery/blob/1.6.2/src/core.js#L296
Example: http://jsfiddle.net/HgS2R/1/
Just be aware that instances of your custom constructor won’t behave identically to an actual Array.
EDIT:
With respect to the edit in your question,
.push()is not identical to.add().The
.push()method is for adding a DOM element to an existing jQuery object.The
.add()method can accept a DOM element, or another jQuery object with 1 or more DOM elements, and it returns a new jQuery object with the new elements added.It’s more like a
.concat().