Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you only want the <code>addProduct</code> and <code>deleteProduct</code> methods to feature on that <em>single</em> jQuery object, then what you've got will work fine; but you'll have to keep a reference to that jQuery object/ only use it once, to preserve the existance of the <code>addProduct</code> and <code>deleteProduct</code> methods. </p> <p>However, these <code>addProduct</code> and <code>deleteProduct</code> methods are unique to that particular jQuery object; the methods won't exist on any other jQuery objects you create;</p> <pre><code>var productList = $.extend($('ul.products'), { addProduct: function(productData) { // add a new li item }, deleteProduct: function(productId) { // delete li with id } }); // Using this particular jQuery object (productList) will work fine. productList.addProduct(); productList.removeProduct(); // However, addProduct() does not exist on new jQuery objects, even if they use // the same selector. $('ul.products').addProduct(); // error; [object Object] has no method 'addProduct' </code></pre> <p>The best way do to this would be to go-back-to-basics and define separate <code>addProduct</code> and <code>deleteProduct</code> functions, which accept a jQuery object. If you wanted to restrict these functions to they only worked on the <code>ul.products</code> selector, you could do;</p> <pre><code>function addProduct(obj) { obj = obj.filter('ul.products'); // do something with `obj` (its a jQuery object) } </code></pre> <p>This approach would be recommended as it keeps the <code>jQuery.fn</code> API consistent; otherwise you'd be adding <code>addProduct</code> and <code>removeProduct</code> to <em>some</em> <code>jQuery.fn</code> instances but not others, or making their usage redundant in others. With this approach however <code>addProduct</code> and <code>removeProduct</code> are always there, but don't get in anyones way if they don't want to use them.</p> <hr> <h1>Historical Notes</h1> <p>This answer was originally written in November 2011, when jQuery 1.7 was released. Since then the API has changed considerably. The answer above is relevant to the <em>current</em> 2.0.0 version of jQuery.</p> <p>Prior to 1.9, a little used method called <a href="http://api.jquery.com/jQuery.sub/" rel="noreferrer"><code>jQuery.sub</code></a> used to exist, which is <em>related</em> to what you're trying to do (but won't help you unless you change your approach). This creates a new jQuery constructor, so you could do;</p> <pre><code>var newjQuery = jQuery.sub(); newjQuery.fn.addProduct = function () { // blah blah }; newjQuery.fn.deleteProduct = function () { // blah blah }; var foo = newjQuery('ul.products'); foo.deleteProduct(); foo.addProduct(); var bar = jQuery('ul.products'); bar.deleteProduct(); // error bar.addProduct(); // error </code></pre> <p>Be careful though, the <code>$</code> method alias would reference the old <code>jQuery</code> object, rather than the <code>newjQuery</code> instance. </p> <p><code>jQuery.sub</code> was removed from jQuery in 1.9. It is now available <a href="http://jquery.com/upgrade-guide/1.9/" rel="noreferrer">as a plugin</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload