TypeScript:Lambdas 并使用“this”

AxD*_*AxD 3 lambda this typescript

JavaScript 框架通常使用 apply() 调用回调。

然而,TypeScript 的箭头符号似乎不允许我访问“this”指针。

它是怎么做的?

如果不是,是否有地方可以对当前在 Lambda 上的“this”处理投反对票?

Dav*_*ret 5

TypeScript 对this箭头函数的处理符合 ES6(阅读:箭头函数)。由于该规范,它以任何其他方式行事都是不一致的。

如果要访问this当前函数,则可以使用常规函数。

例如,更改:

function myScope() {
    var f = () => console.log(this); // |this| is the instance of myScope
    f.call({});
}

new myScope();
Run Code Online (Sandbox Code Playgroud)

到:

function myScope() {
    var f = function() { console.log(this); } // |this| is {}
    f.call({});
}

new myScope();
Run Code Online (Sandbox Code Playgroud)