emit this capturing as a first action in the constructor

This commit is contained in:
Vladimir Matveev 2014-08-01 11:16:09 -07:00
parent 13bda5247b
commit c0b3efbbb4
10 changed files with 51 additions and 11 deletions

View file

@ -1371,6 +1371,7 @@ module ts {
write(" {");
scopeEmitStart(node, "constructor");
increaseIndent();
emitCaptureThisForNodeIfNecessary(node);
if (ctor) {
emitDefaultValueAssignments(ctor);
emitRestParameter(ctor);
@ -1390,8 +1391,7 @@ module ts {
write("_super.apply(this, arguments);");
emitEnd(node.baseType);
}
}
emitCaptureThisForNodeIfNecessary(node);
}
emitMemberAssignments(node, /*nonstatic*/0);
if (ctor) {
var statements: Node[] = (<Block>ctor.body).statements;

View file

@ -114,8 +114,8 @@ var Base = (function () {
var Derived = (function (_super) {
__extends(Derived, _super);
function Derived() {
_super.call(this, function () { return _this; });
var _this = this;
_super.call(this, function () { return _this; });
}
return Derived;
})(Base);
@ -147,8 +147,8 @@ var M2;
var Derived = (function (_super) {
__extends(Derived, _super);
function Derived() {
_super.call(this, function () { return _this; });
var _this = this;
_super.call(this, function () { return _this; });
}
return Derived;
})(Base);

View file

@ -0,0 +1,32 @@
//// [captureThisInSuperCall.ts]
class A {
constructor(p:any) {}
}
class B extends A {
constructor() { super({ test: () => this.someMethod()}); }
someMethod() {}
}
//// [captureThisInSuperCall.js]
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 __();
};
var A = (function () {
function A(p) {
}
return A;
})();
var B = (function (_super) {
__extends(B, _super);
function B() {
var _this = this;
_super.call(this, { test: function () { return _this.someMethod(); } });
}
B.prototype.someMethod = function () {
};
return B;
})(A);

View file

@ -33,8 +33,8 @@ class Test1 {
//// [classMemberInitializerWithLamdaScoping.js]
var Test = (function () {
function Test(field) {
this.field = field;
var _this = this;
this.field = field;
this.messageHandler = function () {
var field = _this.field;
console.log(field);

View file

@ -47,8 +47,8 @@ var Foo2 = (function () {
})();
var Foo3 = (function () {
function Foo3(_this) {
this._this = _this;
var _this = this;
this._this = _this;
var lambda = function () {
return function (x) { return _this; };
};
@ -66,8 +66,8 @@ var Foo4 = (function () {
})();
var Foo5 = (function () {
function Foo5(_this) {
this._this = _this;
var _this = this;
this._this = _this;
var lambda = function () {
return function (x) { return _this; };
};

View file

@ -58,9 +58,9 @@ var Derived2 = (function (_super) {
var Derived3 = (function (_super) {
__extends(Derived3, _super);
function Derived3(a) {
var _this = this;
_super.call(this, function () { return _this; });
this.a = a;
var _this = this;
}
return Derived3;
})(Base);

View file

@ -26,8 +26,8 @@ module M {
//// [lambdaPropSelf.js]
var Person = (function () {
function Person(name, children) {
this.name = name;
var _this = this;
this.name = name;
this.addChild = function () { return _this.children.push("New child"); };
this.children = ko.observableArray(children);
}

View file

@ -36,9 +36,9 @@ var M;
(function (M) {
var C = (function () {
function C(c1, c2, c3) {
var _this = this;
this.c1 = c1;
this.c2 = c2;
var _this = this;
this.x = C.y + this.c1 + this.c2 + c3;
this.g = function (v) { return C.f(_this.x + C.y + v + _this.c1 + _this.c2 + C.pub); };
}

View file

@ -25,8 +25,8 @@ var Base = (function () {
var Super = (function (_super) {
__extends(Super, _super);
function Super() {
_super.call(this, (function () { return _this; })());
var _this = this;
_super.call(this, (function () { return _this; })());
}
return Super;
})(Base);

View file

@ -0,0 +1,8 @@
class A {
constructor(p:any) {}
}
class B extends A {
constructor() { super({ test: () => this.someMethod()}); }
someMethod() {}
}