Monster Z
Monster Z

Reputation: 147

How to extend function in another anonymous function?

All I have following code:

(function($,undefined){
    function Abc(){
      function sayHello(){
          console.log("I am Abc");
      }
    }
})(jQuery);

And my question is, how can I add more methods to Abc or overwrite sayHello? Thanks!

Upvotes: 0

Views: 146

Answers (1)

icktoofay
icktoofay

Reputation: 129001

You can't. It's a local variable, private to that invocation of Abc. It cannot be overridden if Abc is written that way.

If you were actually making methods, perhaps like this:

function Abc() {
    this.sayHello = function() {
        console.log("I am Abc");
    };
}

Then you could extend and override it like so:

function Cba() {
    Abc.apply(this, arguments);
    this.sayHello = function() {
        console.log("I am Cba");
    };
}

Upvotes: 2

Related Questions