0

I have an existing jQuery plugin, now I want to extend it. Consider the below mentioned plugin:

$.fn.x = function(option) {
        var defaults = {
            a: 1,
            b: 2
        };

        option = $.extend(def, option);
        function abc() {
            //do something
        }
        function def() {
            //do something
        }
    };

Now the above one is the plugin I got from somewhere. I need to have custom behavior for abc method, say

function abc() {
                //do something else
            }

I don't want to change the existing plugin, Can you tell me how could I achieve the same by extending the same or by making my own custom plugin ?

1
  • 1
    For information on the difficulty of extending without changing plugins, see this question. Commented Mar 22, 2011 at 14:37

2 Answers 2

1

You will not be able to replace/override the method in this particular situation because it is declared as a local variable. The best solution would be to write your own plugin, or extend this one with an option for abc function. Something like this:

function abc() {
    if(!!option.abc){
         return option.abc.call(this);    
    }
    alert('old abc');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain " or extend this one with an option for abc function" , I don't want to touch the exisiting plugin.
@Rocky Singh - I don't see a way around it because of how the plugin is written. You will need to write your own, or find a different plugin.
0

Think what you're looking for is jQuery extend

newFunctionality = {
    abc: function (){
        alert('yo');
    }
}

$.extend($.fn.x, newFunctionality);
$.fn.x.abc() #=> 'yo'

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.