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 ?

Rocky Singh
  • 15,128
  • 29
  • 99
  • 146
  • 1
    For information on the difficulty of extending without changing plugins, see [this question](http://stackoverflow.com/questions/2050985/best-way-to-extend-a-jquery-plugin). – justkt Mar 22 '11 at 14:37

2 Answers2

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');
}
Josiah Ruddell
  • 29,697
  • 8
  • 65
  • 67
  • 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 Mar 22 '11 at 15:22
  • @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. – Josiah Ruddell Mar 22 '11 at 15:41
0

Think what you're looking for is jQuery extend

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

$.extend($.fn.x, newFunctionality);
$.fn.x.abc() #=> 'yo'
gearsdigital
  • 13,915
  • 6
  • 44
  • 73
Yule
  • 9,668
  • 3
  • 51
  • 72