Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the benefits of es6 Module

Shulou Source: shulou.com Published: 2022-06-02 15:43:55 09月20日 Update

This article mainly introduces "what are the benefits of es6 Module". In daily operation, I believe many people have doubts about the benefits of es6 Module. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the benefits of es6 Module?" Next, please follow the editor to study!

CommonJS loads an object (module.export), which is generated only after the script has been run. ES6 is not an object, but its external interface is a static definition, which is generated during the code static parsing phase.

Differences between es6 Modular module and CommonJS and AMD Modules (loaded at run time)

1. Runtime loading

/ / CommonJS module

Let {stat,exists,readFile} = require ('fs')

/ / equivalent to

Let_fs=require ('fs')

Letstat=_fs.stat

Letexists=_fs.exists

Letreadfile=_fs.readfile

The essence of the above code is to load the fs module as a whole (that is, load all the methods of fs), generate an object (_ fs), and then read three methods from that object. This kind of loading is called "runtime loading" because this object is available only at run time, making it impossible to do "static optimization" at compile time.

2. ES6 can complete module loading at compile time.

one

two

/ / ES6 module

Import {stat,exists,readFile} from'fs'

The essence of the above code is to load three methods from the fs module, while the other methods are not loaded. This kind of loading is called "compile-time loading" or static loading, that is, ES6 can complete module loading at compile time, which is more efficient than CommonJS module loading. Of course, this also makes it impossible to reference the ES6 module itself, because it is not an object.

Benefits of ES6 module module

1. Because the ES6 module is loaded at compile time, static analysis is possible. With it, you can further broaden the syntax of JavaScript, such as introducing macros (macro) and type checking (typesystem), which can only be implemented by static analysis.

2. UMD module format is no longer needed, and ES6 module format will be supported by servers and browsers in the future. At present, this has already been done through a variety of tool libraries.

3. In the future, the new API of the browser can be provided in module format, and it no longer has to be made as a global variable or a property of the navigator object.

4. Objects are no longer needed as namespaces (such as Math objects). These functions can be provided through modules in the future.

At this point, the study of "what are the benefits of es6 Module" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Modules objects static benefits methods compilation run learning code format generation function only substance more browser analysis help browsing utility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Xiaomi Microsoft Shulou Information