Reputation: 3343
Say I have a following:
module Test {
export class Foo {
public A = 123;
public GetA() {
return this.A;
}
}
}
which compiles to
var Test;
(function (Test) {
var Foo = (function () {
function Foo() {
this.A = 123;
}
Foo.prototype.GetA = function () {
return this.A;
};
return Foo;
})();
Test.Foo = Foo;
})(Test || (Test = {}));
And its all great, but I have a case when GetA will be run in a context of different object, so I need to capture 'this' in a closure.
So basically I need to have this JS:
function Foo() {
this.A = 123;
var self = this;
this.GetA = function () {
return self.A;
}
}
Is there a way to get this with class semantics of typescript, of should I just fallback to plain JS?
Upvotes: 0
Views: 1138
Reputation: 430
For those who encounter this page, apparently TypeScript people adressed this here: https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript
Upvotes: 2
Reputation: 13399
module Test {
export class Foo {
public A = 123;
public GetA = ()=>{return this.A;}
}
}
Please give it a try. This is the javascript generated:
var Test;
(function (Test) {
var Foo = (function () {
function Foo() {
var _this = this;
this.A = 123;
this.GetA = function () {
return _this.A;
};
}
return Foo;
})();
Test.Foo = Foo;
})(Test || (Test = {}));
Upvotes: 1