Javascript Class Inheritance For Functions

前端 未结 2 1920
你的背包
你的背包 2020-11-27 22:02

I have set up a base class as standard:

MyBase = function() {
    this.m_Stuff = 0; // etc
};
MyBase.prototype.MySuperFunction = function (arg1) {
    alert(         


        
相关标签:
2条回答
  • 2020-11-27 22:27

    It's similar to the call in your inherited constructor. You can access the "super" method still on MyBase.prototype.MySuperFunction (where you assigned it), so use:

    MyBase.prototype.MySuperFunction.call(this, arg1);
    

    For a more dynamic approach you even might use Object.getPrototypeOf to get the prototype, but watch out that it works with dynamic inheritance. And if you have many methods that need to call their parent, it can be helpful to alias MyBase.prototype as a super variable which is accessible to all functions on the Child prototype object (see this answer for an example)).

    0 讨论(0)
  • Please apply the following:-

    MyBase.prototype.MySuperFunction.call(this, arg1);
    
    0 讨论(0)
提交回复
热议问题