DJCordhose
DJCordhose

Reputation: 148

New version of __extends generated by TypeScript compiler

When you use inheritance, the TypeScript compiler generates the __extends function for you. Older versions of the tsc compiler generated something like this

var __extends = this.__extends || function (d, b) {
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};

which sets an instance of b as the prototype chain of d. That is pretty much what I would to by hand as well.

The most recent version (0.9) adds copying of property/method references which looks superflous be me:

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 __();
};

Does anyone know the reason for this?

Upvotes: 2

Views: 956

Answers (1)

basarat
basarat

Reputation: 276085

It now respects static properties on classes as well.

The key statment is:

for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];

which will copy the parent class static members to child classes.

e.g.:

class Foo{
    static x = "asdf";
}

class Bar extends Foo{

}

alert(Bar.x);

Try it

The original (now closed) bug report : http://typescript.codeplex.com/workitem/825

Upvotes: 4

Related Questions