2

I have a javascript Object like below:

var Loader = function() {}
Loader.prototype = {
    constructor: Loader,
    foo: function(a, b) {
        function bar() {
            return a + b;
        }
        return bar();
    }
};

I need to override the bar() function present inside foo . The foo function can be overridden as:

Loader.prototype.foo = function() {}

I have tried the same for inner function but no luck.

Loader.prototype.foo.bar = function() {}//doesnt work
Loader.prototype.foo.protoype.bar = function() {}//doesnt work

I would be very happy if someone could point me in the right direction. Thanks in advance

2
  • 1
    You cannot override the inner function because it's scope is restricted to the function foo. Commented May 5, 2016 at 5:45
  • I guess you will have to override (copy paste) the entire foo() function and only change the relevant parts. Yes, it can be bad if updates arrive for what you are overriding (a 3rd party lib, perhaps). Commented May 5, 2016 at 5:50

2 Answers 2

3

In the current declaration, the bar() function cannot be modified. It exists in the scope of foo() method and cannot be changed from outside.


An alternative solution for a dynamic override could be:

var Loader = function() {}
Loader.prototype = {
    constructor: Loader,
    foo: function(a, b, barFun) {
        if (typeof barFun !== 'function') {
           barFun = function() {
              return a + b;
           }
        }
        return barFun.call(this, a, b);
    }
};

When calling foo(), it's possible to use a custom function as the 3rd parameter.
Then you can use it:

var loader1 = new Loader();

loader1.foo(1, 2); // prints 1 + 2 = 3, default bar() function

loader1.foo(1, 2, function(a, b) {
  return a - b;
}); // prints 1 - 2 = -1, custom bar() function

Check the working demo.

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

6 Comments

Thanks for the quick answer, looks like i will have to go with this approach.
But bar can effectively be replaced by just assigning a different function to foo that uses different code than what bar is doing and this can be done from the outside.
@jfriend00 which means if I have 60 different functions inside foo() i will have to override them all.
@jfriend00 This is a skeleton example. Before calling bar(), the foo() may contain a lot of logic. Simply overriding it will create duplications.
@jfriend00 I wanted to override buildImage() inside parse() in this : github.com/mrdoob/three.js/blob/master/examples/js/loaders/…. It was much easier to ask with a simpler example.
|
0

In my opinion,the inner bar function is not the method of the outter foo function which is the method of loader object.Hope that it will help you

2 Comments

No this doesn't help. bar() is function of foo() which is function of Loader.
I just meant that it's not the method of it.

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.