util.inherits - alternative or workaround

后端 未结 4 759
面向向阳花
面向向阳花 2021-02-05 08:46

I am a n00b in node, and find util.inherits() very useful, except for the fact that it seems to replace the entire prototype of the original object. For instance:

4条回答
  •  再見小時候
    2021-02-05 09:15

    You should first inherit, after the function definition, and then implement your object. Also don't forget to call the superclass constructor in your class's constructor.

    Function A(y){
      this.y = x;
    }
    
    Function B(x,y){
      this.x = x;
      A.call(this,y);
    }
    
    util.inherits(B,A);
    
    B.prototype.mythodA = function() {
        //do something
    }
    

提交回复
热议问题