【发布时间】:2015-11-14 16:50:55
【问题描述】:
我有一些 Typescript 代码,看起来像这样:
class Test {
private userId: string
constructor() {
this.userId = 'test'
}
test() {
return new Promise((resolve, reject) => {
let sharing = 'private'
console.log("before switch this.userId", this.userId);
switch (sharing) {
case "private":
resolve(this.userId);
break;
}
})
}
}
如果我在 the Typescript playground 中编辑它,则会按预期发出以下 Javascript 代码:
var Test = (function () {
function Test() {
this.userId = 'test';
}
Test.prototype.test = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var sharing = 'private';
console.log("before switch this.userId", _this.userId);
switch (sharing) {
case "private":
resolve(_this.userId);
break;
}
});
};
return Test;
})();
但是,如果我在已安装的 Atom 编辑器中编辑相同的代码,则会发出:
var Test = (function () {
function Test() {
this.userId = 'test';
}
Test.prototype.test = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var sharing = 'private';
console.log("before switch this.userId", _this.userId);
switch (sharing) {
case "private":
resolve(this.userId); // This resolve is missing a _
break;
}
});
};
return Test;
})();
不同之处在于解析语句向下 12 行。正在解决的是this.userId,而不是_this.userId。
我正在运行新安装:
- Atom 1.1.0
- 原子打字稿 7.8.0
- Ubuntu 15.10
- typescript 1.6.2 与 npm 一起安装
【问题讨论】: