Questions about __extends

Topics: General
Jan 19, 2014 at 6:01 PM
I've been looking at the __extends shim that tsc inserts for derived classes and I'd like to ask about it's current implementation:
var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};

Static properties in derived classes

As far as I understand it the following piece:
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
copies static properties from base class to derived class. Why does it do that? Is there a use case when it's better to use Derived.staticField instead of Base.staticField? Or maybe it's mimicking the way Java and C# allow access to static fields through derived classes?

this.constructor

This line sets up the constructor in instances of derieved classes:
    function __() { this.constructor = d; }
but in TypeScript code it's impossible to access constructor field directly (this.constructor fails to compile) so is this code executed for other JavaScript code that may be running (just in case someone wants to do reflection)? I know that without it the constructor property will point to the Base function but it's not possible to access constructor property in TS.

Object.create

Also this code:
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
can be shortened to:
d.prototype = Object.create(b.prototype);
d.prototype.constructor = d; // if constructor matters
in browsers supporting ES5: http://kangax.github.io/es5-compat-table/#Object.create

Thank you in advance for the explanation!