Typescript - 自执行匿名函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30274464/
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 03:00:47  来源:igfitidea点击:

Typescript - Self-Executing Anonymous Functions

javascripttypescript

提问by Mudaser Ali

How I can create self-executing anonymous functions using type script?

如何使用类型脚本创建自执行匿名函数?

For example

例如

(function() {
 var someClass = {
 }
}.call(this));

I want a built a plugin that may work for Node.js, also for fron-tend as well.

我想要一个内置的插件,它可能适用于 Node.js,也适用于前端。

回答by Vadorequest

/**
 * Self executing anonymous function using TS.
 */
(()=> {
    // Whatever is here will be executed as soon as the script is loaded.
    console.log('executed')
})();

I want a built a plugin that may work for Node.js, also for fron-tend as well.

我想要一个内置的插件,它可能适用于 Node.js,也适用于前端。

In that case you should compile your TypeScript in AMD and use an AMD loader on the frontend, like http://requirejs.org/docs/start.html

在这种情况下,您应该在 AMD 中编译您的 TypeScript 并在前端使用 AMD 加载器,例如http://requirejs.org/docs/start.html

On the server side, you would need to use requirejsnode package as well to load the file. Take a look at this: http://requirejs.org/docs/node.html

在服务器端,您还需要使用requirejs节点包来加载文件。看看这个:http: //requirejs.org/docs/node.html

Basically there are two ways to compile TS to JS, using AMD, which is browser compliant, or using CommonJS, which is node.js compliant. Loading an AMD script in the browser or in the server needs to use an AMD compliant loader, and *requirejs** is one of them. (the most famous/used I'd say)

基本上有两种方法可以将 TS 编译为 JS,使用兼容浏览器的 AMD,或者使用兼容 node.js 的 CommonJS。在浏览器或服务器中加载 AMD 脚本需要使用 AMD 兼容的加载器,*requirejs** 就是其中之一。(我想说的是最著名/最常用的)

回答by Adi

First rule in TypeScript: Any valid JavaScript is valid TypeScript.

TypeScript 的第一条规则:任何有效的 JavaScript 都是有效的 TypeScript。

No, there are are no special way to write Self-Executing Anonymous Functions in TS as of right now.

不,目前没有在 TS 中编写自执行匿名函数的特殊方法。

But, below is some code that might be of some use in your situation.

但是,下面是一些可能对您的情况有用的代码。

Every Class in TS is compiled to a (Named) Self-Executing Anonymous Functions, which returns a function.

TS 中的每个类都被编译成一个(命名的)自执行匿名函数,它返回一个函数。

Example:

例子:

//ts
class someClass {
  someProperty = "this is a property";
}

Translates to

翻译成

//js
var someClass = (function () {
    function someClass() {
        this.someProperty = "this is a property";
    }
    return someClass;
})();

Hope this is of some help.

希望这个对你有帮助。

回答by PeteC

An self-executing, immediate, recursive top-level React function in Typescript:

Typescript 中的自执行、立即、递归的顶级 React 函数:

(function handleAutomatonTypeChange(newtype: AutomatonType) {
  ReactDOM.render(
    <Automaton automatonType={newtype}
               onAutomatonTypeChange={handleAutomatonTypeChange} />,
    document.getElementById('automaton')
  );
})('diadic');

回答by jagadeesh

(function(){
    document.body.innerHTML = "Self Calling Function";
}.call(this));