类继承在typescript编译后prototype的实现方法

extends.ts

class E {

  constructor(private name: string, private age: number) {
  }

  sayHello() {}
}

class F extends E {

  constructor(name: string, age: number, private score: number) {
    super(name, age)
  }

  sayHello() {}
  sayGoodbye(){}
}

let f = new F('Rick.Xu', 33, 100)
let e = new E('Ashley', 23);

extends.js

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());

        // test by Rick
        console.log(__.prototype == b.prototype) // true
        console.log(d.prototype instanceof __) // true
        var a = new __()
        a.sayHello()
        // a.sayGoodbye() // a.sayGoodbye is not a function
        // test by Rick end
    };
})();
var E = /** @class */ (function () {
    function E(name, age) {
        this.name = name;
        this.age = age;
    }
    E.prototype.sayHello = function () { };
    return E;
}());
var F = /** @class */ (function (_super) {
    __extends(F, _super);
    function F(name, age, score) {
        var _this = _super.call(this, name, age) || this;
        _this.score = score;
        return _this;
    }
    F.prototype.sayHello = function () { };
    F.prototype.sayGoodbye = function () { };
    return F;
}(E));
var f = new F('Rick.Xu', 33, 100);
var e = new E('Ashley', 23);

typescript 编译后成 ES3后,原型链中加了一个中间对象 __ 。子类的原型对象是 ____的原型对象是父类的原型对象。和下面的方法等效:

d.prototype = Object.create(b.prototype); 

https://xhope.top/?p=687