简体   繁体   English

ES6模块语法和Typescript模块语法之间的混淆

[英]Confuse between ES6 module syntax and Typescript module syntax

All: 所有:

I am pretty new to both ES6 and TypeScript, but I am learning and comparing them parallel right now. 我对ES6和TypeScript还是很陌生,但是现在我正在并行学习和比较它们。

The first thing confuses me so much is there are many similarity overlaps, for example, in terms of the namespace and module( like import/from/export etcs): 首先让我感到困惑的是,有很多相似之处,例如在名称空间和模块方面(例如import / from / export等):

I wonder if anyone could give me a brief comparison summary about the usage of them. 我想知道是否有人可以给我关于它们的用法的简短比较摘要

I also wonder if there is relationship between Typescript and ES6 ? 我也想知道Typescript和ES6之间是否存在关系

Thanks 谢谢

This question might be better suited for the Programmers section of StackExchange, but here we go. 这个问题可能更适合StackExchange的“ 程序员”部分 ,但在这里开始。


There are two types of modules in Typescript, internal modules (namespaces) and external modules. Typescript中有两种类型的模块 ,内部模块(命名空间)和外部模块。 The syntax of the latter is equivalent to the ES6 module syntax. 后者的语法等效于ES6模块的语法。

Internal Modules 内部模块

Also called namespaces. 也称为名称空间。 Internal modules are used when the module is to be compiled along a project, and are primarily a tool for separation concerns, much akin to the use of namespaces in C#. 内部模块用于在项目中编译模块时使用,它主要是用于解决分离问题的工具,这与C#中使用命名空间非常相似。 When compiled with the TypeScript compiler, internal modules are put into closures (via self-invoking functions). 使用TypeScript编译器进行编译时,内部模块将放入闭包中(通过自调用函数)。

MyApp.ts 我的应用程式

namespace MyApp {
    export class MyClass { }
}

... when compiled into ES5, becomes the following abomination: ...当编译成ES5时,会变成以下可憎之处:

MyApp.js MyApp.js

// ES5 compatible
var MyApp;
(function (MyApp) {
    var MyClass = (function () {
        function MyClass() { }
        return MyClass;
    })();
    MyApp.MyClass = MyClass;
})(MyApp || (MyApp = {}));

When compiled into ES6, becomes a bit cleaner, as there is native syntax for classes: 当编译到ES6中时,它变得更加整洁,因为类具有本机语法:

MyApp.js MyApp.js

// ES6 compatible
var MyApp;
(function (MyApp) {
    class MyClass { }
    MyApp.MyClass = MyClass;
})(MyApp || (MyApp = {}));

Internal modules are especially useful when you compile your entire codebase into a single output file. 当您将整个代码库编译为单个输出文件时,内部模块特别有用。


External Modules 外部模组

External modules are compiled separately (per file), and are to be loaded with a module loading system (like RequireJS in ES5, or natively in ES6) during runtime. 外部模块被单独编译(每个文件),并且模块加载系统加载 (如在ES5,或在本地ES6 RequireJS)在运行时间期间。 There is no top-level module declaration, as a top-level export or import statement itself will instruct the compiler that the file itself is a module, and should be compiled accordingly. 没有顶层模块声明,因为顶层导出或导入语句本身将指示编译器该文件本身是一个模块,应相应地进行编译。

When compiled with the TypeScript compiler, external modules are wrapped into the selected module syntax. 使用TypeScript编译器进行编译时,外部模块将包装到选定的模块语法中。 Currently supported are AMD, CommonJS, UMD, System on the ES5 platform, and the native syntax on the ES6 platform. 当前支持的是ES5平台上的AMD,CommonJS,UMD,System,以及ES6平台上的本机语法。

MyApp.ts 我的应用程式

export class MyClass { }

... when compiled into ES5 - with say, UMD - becomes the following blasphemy : ...当被编译成ES5时-例如UMD-成为以下亵渎

MyApp.js MyApp.js

// ES5 compatible
(function (deps, factory) {
    if (typeof module === 'object' && typeof module.exports === 'object') {
        var v = factory(require, exports); if (v !== undefined) module.exports = v;
    } else if (typeof define === 'function' && define.amd) {
        define(deps, factory);
    }
})(["require", "exports"], function (require, exports) {
    var MyClass = (function () {
        function MyClass() { }
        return MyClass;
    })();
    exports.MyClass = MyClass;
});

When compiled into ES6 however, the resulting code becomes infinitely cleaner, as the TypeScript module syntax is based on (equivalent to) the ES6 module syntax: 但是,当编译为ES6时,由于TypeScript模块语法基于(相当于)ES6模块语法,因此生成的代码将变得更加干净。

MyApp.js MyApp.js

// ES6 compatible
export class MyClass { }

However, the ES6 platform is not widely supported yet, so I suggest going with ES5 compilation. 但是,尚未广泛支持ES6平台,因此我建议使用ES5编译。 This comes with the expense of additional generated boilerplate code, but besides the marginally increased bandwidth there are no additional side effects: the same codebase can be compiled into both ES5 and ES6, with functionally identical results. 这会产生额外的生成样板代码,但除了带宽略有增加外,没有其他副作用:相同的代码库可以编译到ES5和ES6中,并在功能上完全相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM