JS Module specification

Source: Internet
Author: User

After the ES6 standard is released, module becomes the standard, the standard use is to export the interface with export instruction, import into the module, but in our usual node module, we use the COMMONJS specification, using require to introduce the module, Export the interface using Module.exports. Node's module follows the COMMONJS specification, Requirejs follows the Amd,seajs follow the CMD, although each has a different, but in short still want to maintain a more uniform code style. ES6 released module does not directly adopt COMMONJS, even require is not adopted, that is, require is still just a private global method of node, Module.exports is also just a global variable attribute of node's private, which has nothing to do with the standard half-dime relationship. Require is run-time, import it is compile-time, it must be placed at the beginning of the file, and the use of the format is also determined. In JavaScript ES6, the export default and export differences: 1.export and export default can be used to export constants, functions, files, modules, etc. 2. You can pass import+ (constant | function |) in other files or modules. Pieces | module) name, and import it so that it can be used 3. In a file or module, export, import can have multiple, export default has only one 4. Export by exporting, add {},export The default does not require 1,
// Demo1.js Const ' Hello World ' export function f (a) {    return A +1}

The corresponding import method:

// Demo2.js  from ' Demo1 ' // can also be written separately two times, when imported with curly braces

Import needs to know the identity of the variable exposed by the Demo.js, otherwise it cannot be loaded

2.

// Demo1.js default Const ' Hello World '

The corresponding import method:

// Demo2.js  from ' Demo1 ' // no curly braces at the time of import

Using the Export default command, specify the default output for the module so that you do not need to know the variable name of the module to be loaded

JS Module specification

Related Article

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.