Subclassing a class with required parameters in JavaScript

后端 未结 3 784
情歌与酒
情歌与酒 2021-02-04 10:07

If subclassing a \"class\" in JavaScript is done like so:

var ParentClass = function() {
    // something
};


var ChildClass = function() {
    // something
};
         


        
3条回答
  •  旧巷少年郎
    2021-02-04 10:43

    A better way to inherit...

    var inherit = (function () {
      var F = function () {}; // cache function
      return function (C, P) { // Accepts Constructor and Parent
        F.prototype = P.prototype;
        // faster prototype chain lookup than direct instantiation
        C.prototype = new F(); 
        C._super = P.prototype;
        C.prototype.constructor = C; // for checking instanceof
      };
    }());
    

提交回复
热议问题