tomsseisums
tomsseisums

Reputation: 13367

`add` but with "mutating" properties, like `push`

Is there a default method in jQuery that allows adding elements to stack without reassigning variable?

For now I'm always doing - $stack = $stack.add($element);, but I hate that reassignment all the times.

Is there something like Array.prototype.push for jQuery?

P.S. Yes, I know I can make a extension, just wondering about default method.

Upvotes: 2

Views: 76

Answers (1)

Bergi
Bergi

Reputation: 665070

Is there something like Array.prototype.push for jQuery?

No, not "something like". There is Array.prototype.push available on the jQuery prototype

var push = [].push;
jQuery.fn = jQuery.prototype = {
    …
    // For internal use only.
    // Behaves like an Array's method, not like a jQuery method.
    push: push,
    …
};

Notice that it is not chainable, but returns the length of the collection.

Upvotes: 3

Related Questions