Expected behaviour of __extends with static accessors? (original) (raw)
The following code:
var i = 0, j = 0;
class Base { get instanceAccessor() { return ++i; } static get staticAccessor() { return ++j; } toString = () => 'instance: ' + this.instanceAccessor + ' static: ' + Base.staticAccessor; }
class Derived extends Base { toString = () => 'instance: ' + this.instanceAccessor + ' static: ' + Derived.staticAccessor; }
console.log('Base:\n%s\n%s\n%s', new Base(), new Base(), new Base()); console.log('Derived:\n%s\n%s\n%s', new Derived(), new Derived(), new Derived());
generates the following output (with the v1.3 compiler):
Base:
instance: 1 static: 2
instance: 2 static: 3
instance: 3 static: 4
Derived:
instance: 4 static: 1
instance: 5 static: 1
instance: 6 static: 1
Clearly Derived.staticAccessor
is no longer an accessor, but a 'snapshot' of the value of Base.staticAccessor
when the Derived
constructor function called __extends
.
Just wondering if this is the expected/correct behaviour?
PS. I haven't run into this problem 'in the wild' as I rarely use classes or accessors, but merely came across a rant/blog post about another compile-to-JS language doing something similar, and was curious how TypeScript handled it.