dfilkovi
dfilkovi

Reputation: 3081

How to extend this jQuery function

I want to extend this function and make my own, this new function should have same functions as the old one but additionalOkClicked should be overwritten with my own function.

How do I do this?

   (function ($) {
    $.LocationDialog = {
        mainInit: function()
        {
            //some code here
        },

        init: function(callingNum)
        {
            //some code here
        },

        additionalOkClicked: function()
        {
            //i want to override this function
        }
    };
})(jQuery);

Upvotes: 0

Views: 348

Answers (3)

Shaz
Shaz

Reputation: 15867

Use prototype:

$.locationDialog.prototype.functionName = function() {
    // Your code here
}

or

$.locationDialog.additionalOkClicked.prototype.functionName = function() {
    // Code here
}

or just create another function inside of $.locationDialog:

   (function ($) {
    $.LocationDialog = {
        mainInit: function()
        {
            //some code here
        },

        init: function(callingNum)
        {
            //some code here
        },

        additionalOkClicked: function()
        {
            //some code here
        }

        functionName: function() {
            // Your code here
        }
    };
})(jQuery);

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57268

can't you just do:

$.LocationDialog.myAdditionalOnClick = function()
{
    this.additionalOnClick(); //call the other method in the object.
}

if y our talking about the function being bound to an event then use:

$('#foo').unbind('click',$.LocationDialog.additionalOnClick);
$('#foo').bind('click',$.LocationDialog.myAdditionalOnClick);

Upvotes: 1

mVChr
mVChr

Reputation: 50185

You can just add code to delete that function and then create your own with the same name:

(function ($) {
  delete $.LocationDialog.additionalOkClicked;

  $.LocationDialog.additionalOkClicked = function () {
    // your code here
  };
})(jQuery);

The rest of $.LocationDialog object will remain the same.

Upvotes: 0

Related Questions