3

I have a method addObject on a jQuery prototype which I have done using jQuery.fn.extend().

jQuery.fn.extend({
    addObject : function(objectKey){
        this[objectKey] = {};
    }
});

Now I want to add a method addNewObject to its prototype. I am trying to emulate the below scenario

function addObject(){
    ...
}

addObject.prototype.addNewObject = function(){
    ...
}

How is my addObject accessible with new keyword?

3
  • 2
    "I have a method addObject on jquery prototype which I have done using jQuery.fn.extend()." Show us, don't tell us. Commented Jan 24, 2016 at 15:23
  • 2
    jQuery.fn.addObject.prototype.addNewObject = function() {... Commented Jan 24, 2016 at 15:31
  • What is expected result of this[objectKey] = {}; ? Commented Jan 24, 2016 at 15:40

1 Answer 1

2

Instead of using extend you could use:

// insulate plugin in IIFE
(function() {
    // declare function
    function addObject(options) {
        return this.each(function(){});
    }
    // add prototypes
    addObject.prototype.addNewObject = function() {}

    // extend as plugin by passing function reference
    $.fn.addObject = addObject;


})(jQuery);

See https://jqueryboilerplate.com/ for lots of information, guides , patterns etc for developing plugins

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.