Calling parent functions in JavaScript
Today I was working with JavaScript objects and I had to do something that required calling a parent function from the subclass function that overrides it. So I had a parent class and a subclass:
I tried a few things, like Douglas Crockford’s inheritance patterns, but they wouldn’t give me access to Parent.myPrivate from within Parent.myFunc. The problem had to do with scope: they would use Function.apply to call Parent.myFunc, using SubClass’s scope. JavaScript doesn’t give inherited classes access to the parent classes’ private variables. So something like this doesn’t work:
I found a bunch of other ways to deal with inheritance, none of which worked the way I needed. I ended up doing having to make an alias for Parent.myFunc before defining the overridding method. Here’s the entire source: