打字稿 - 自执行匿名函数

Mud*_*Ali 6 javascript typescript

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

例如

(function() {
 var someClass = {
 }
}.call(this));
Run Code Online (Sandbox Code Playgroud)

我想要一个可以用于Node.js的内置插件,也适用于fron-tend.

Vad*_*est 23

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

我想要一个可以用于Node.js的内置插件,也适用于fron-tend.

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

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

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