Es6-module and Commonjs

Source: Internet
Author: User
Tags export class

Objective

Many years ago, we introduced code that was introduced directly with script SRC, or wrote a loadscript on its own.

With script Src Way, when the project JS file more, there will be a lot of script tags on the page, put aside performance for the time being, the code is very ugly, and very poor maintenance, especially when the JS file has a dependency, more difficult to deal with.

In order to solve this pain point, to handle the load and dependency of JS, and to promote JS modular development, so there is a browser-side CMD, AMD and other specifications for the service side (such as Nodejs) COMMONJS specifications, as well as the final standard ES6 module specification. Get a rough look at these things first.

Cmd:common module Definition-Public module definitions, is Mr. Yuber's seajs in the promotion process of the module definition of the normalized output.

Amd:asynchromous Module Definition-asynchronous modules is defined as the normalized output that Requirejs defines for a module during the promotion process.

COMMONJS Specification: Service-side specification, file-as-a-module. Export the module with Module.exports, require introduce the file/module.

ES6 MODULE:ES6 module Standard.

Grammar and use

CMD and AMD do not make too much of a description. Want to know about the children's shoes please search in the morning. Here the main records ES6 Module and CommonJs

ES6 Module


The ES6 Module automatically adopts strict mode, regardless of whether you have a wood and add ' use strict ' to the code.

Export exported variables, import imported variables. You should be familiar with children's shoes after the end of the language.

//   export can derive variables, objects, functions, and classes.
= ' Linux '= ' captain '= ' pwd '; export { username, password = (x) + x + x; Export Class fun{}

Note that the Export command specifies an external interface, so ensure that the interface name must establish a one by one corresponding relationship with the internal variables of the module. The meaning of this sentence is that you cannot define a good variable, just export the variable directly.

Either export at the time of definition, or export an object at the end of the file, which is the variable to be exported and a key-value pair relationship.

// error export 1; // correct Const T = 1; export {    T}// error Const TEST = () = {}export test; // correct export TEST = () = {}export {    Test  }

Es6-module and Commonjs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.