0

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);
2
  • Why don't you just delete the code instead additionalOkClicked and write your own ? Commented Mar 19, 2011 at 17:12
  • I need to EXTEND this function to override a method inside not to create new or edit the same function. I need extending cause old will be used the same but new function will be used in special case Commented Mar 19, 2011 at 17:18

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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);

4 Comments

I need to EXTEND this function to override a method inside not to create new or edit the same function. I need extending. Prototype will change existing method, so old function won't work as expected, i need both cases.
@dfilkovi the prototype syntax that Shaz mentioned first should work then.
No it won't work cause old function will be changed and if someone want's to use previous $.LocationDialog it will be with new function, I need new $.LocationDialogExtended with new function, I don't want to change old function.
and it should be var functionName = function() {

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.