0

Given:

class = function class(...) {

    var foo = function(...) {...};

    bar: function(...) {...};

    baz: function(...) {...};

}

In another file I'm overriding class.bar via:

class.bar = function(...) {

    myStuff();

    this.foo(...);  

};

But this gives me the error: this.foo is not a function

user3280015
  • 279
  • 2
  • 10
  • 3
    You can't, without exposing it publically. – Matt Jul 21 '14 at 09:37
  • Why....? Give me some trick ;) I cannot change the original code. – user3280015 Jul 21 '14 at 09:38
  • 2
    ... because of the nature of it being private. `foo` isn't part of `this`. `foo` is a named function captured in the closure around `bar`. When you overwrite `bar`, there is no way to get access to the scope of the previous `bar`, and therefore call `foo`. There is **no** way this can be achieved. Period. – Matt Jul 21 '14 at 09:40
  • "there is no way to get access to the scope of the previous bar", is this quite true? Because `this.baz(...)` works from within the override. – user3280015 Jul 21 '14 at 09:45
  • 1
    Yes, it's true. `this` isn't part of the scope, it's part of the execution context when you call `class.bar()`. In that particular invocation, `this` inside `bar` is `class`. Since you expose `baz` publically (on `class`), `this.baz(...)` works. You *haven't* exposed `foo` on `class`, so you can't use `this` trick (pun intended). Sorry if this all sounds cryptic. I'm trying to be as clear as possible, but it's quite hard to explain *why*. – Matt Jul 21 '14 at 09:49
  • You might want to read [about the difference between variables and properties](http://stackoverflow.com/q/13418669/1048572). – Bergi Jul 21 '14 at 11:15

0 Answers0