0

I have the original.js file, but I want to change some things in it. I can't modify original.js, but I can add another better.js file so that I could overwrite some functions of original.js

original.js contains:

MyHandler = {
   data:{},
   var1:false;
   handlers:{},

   init:function(handlers){
       function1();
       function2();
   }
   function1:function()
   {
    // function1 code that needs to be replaced
   };
}

$(document).ready(function ()
{
///...some code
      MyHandler.init();
}

I want to rewrite function1() with new content. What should I put within better.js file?

P.S. I know better.js should follow after original.js.

I've tried to put the code below to better.js, but it doesn't work (seems like none of function1 work then)

MyHandler = {
   function1:function()
   {
    // new code
   };
}

What am I doing wrong?

1
  • You'r problem does not seem to have anything to do with jQuery. There is no such thing as a "jQuery function". Commented Oct 28, 2012 at 18:25

1 Answer 1

3

Just do like this:

if (!MyHandler) { MyHandler = {};}
MyHandler.function1 = function() { ... };

Or you can use jQuery $.extend api method.

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

3 Comments

+1, this is how it can be done. You should note that the new function will not be able to access the internal data (e.g. MyHandler.data).
@El: Depends on how the function is called and the actual setup. There is not enough information to make such a statement. It looks like the function could easily access MyHandler.data because MyHandler appears to be global. Or if the function is called with MyHandler.function1(), the property can be accessed with this.data.
You're quite right; I glanced too quickly and thought it was the revealing module pattern.

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.