Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the benefits of es6 Module

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report