Understanding the __extends function generated by typescript? : <p>I am playing with <a href="https://www.typescriptlang.org/play/#src=class%20A%20%7B%20%7D%0D%0Aclass%20B%20extends%20A%20%7B%20%7D" rel="noreferrer">Typescript</a> and trying to understand the compiled Javascript code generated by the compiler</p>

<p>Typescript code:</p>

<pre><code>class A { }
class B extends A { }
</code></pre>

<p>Generated Javascript code:</p>

<pre><code>var __extends = (this &amp;&amp; this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array &amp;&amp; function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var A = /** @class */ (function () {
    function A() {
    }
    return A;
}());
var B = /** @class */ (function (_super) {
    __extends(B, _super);
    function B() {
        return _super !== null &amp;&amp; _super.apply(this, arguments) || this;
    }
    return B;
}(A));
</code></pre>

<p>The Javascript inheritance as per <a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance" rel="noreferrer">Mozilla docs</a> is this:</p>

<pre><code>B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
</code></pre>

<p>The parts that I do not understand in the Typescript's generated code are this</p>

<p>1. What is the purpose of this line? Looks like it is copying all the keys of A into B? Is this some sort of a hack for static properties? </p>

<pre><code>var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array &amp;&amp; function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
</code></pre>

<p>2. What is this doing?</p>

<pre><code>function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
</code></pre>

<p>I don't understand this part: <code>(__.prototype = b.prototype, new __())</code></p>

<p>Why does function B() return this?</p>

<pre><code>return _super !== null &amp;&amp; _super.apply(this, arguments) || this;
</code></pre>

<p>If someone can explain this to me line by line I would be grateful.</p>
