我有一些看起来像这样的 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 行的 resolve 语句。正在解决的是 this.userId
而不是 _this.userId
。
我正在运行一个新安装:
- 原子 1.1.0
- 原子 typescript 7.8.0
- Ubuntu 15.10
- 使用 npm 安装 typescript 1.6.2
最佳答案
这是两天前修复的 TypeScript 问题:https://github.com/Microsoft/TypeScript/issues/5637
刚刚在 atom-typescript 中修复了 https://github.com/TypeStrong/atom-typescript/releases/tag/v7.10.0
关于typescript - Typescript 1.6.2 和 atom-typescript 的 switch 语句中的 'this' 值是否发出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33710856/