TypeScript 匿名函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20700155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:52:43  来源:igfitidea点击:

TypeScript anonymous function

javascripttypescriptanonymous-function

提问by Matthew Layton

What is the TypeScript equivalent of this JavaScript?

这个 JavaScript 的 TypeScript 等价物是什么?

(function() { 
    /* code here */ 
})();

I have tried this

我试过这个

() => {
    /* code here */
}

But this produces

但这会产生

(function() {
    /* code here */
});

I need the extra set of parenthesis at the end to perform an execution of the anonymous function.

我需要最后一组额外的括号来执行匿名函数。

回答by Jude Fisher

(() => {
    /* code here */
})();

or simply use the JavaScript (which is equally valid TypeScript)

或者简单地使用 JavaScript(同样有效的 TypeScript)

(function() { 
    /* code here */ 
})();

... depending whether you want to capture thisusing the fat arrow.

...取决于您是否要this使用粗箭头进行捕获。

Playground.

游乐场