What are the module specifications of JavaScript?

青灯夜游
Release: 2022-02-18 18:30:02
Original
3193 people have browsed it

JavaScript module specifications include: 1. CommonJS specification; 2. AMD (Asynchronous Module Definition) specification; 3. CMD (Common Module Definition) specification; 4. UMD specification (a blend of AMD and CommonJS).

What are the module specifications of JavaScript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

There are three common JavaScript modular specifications, CommonJS, AMD (Asynchronous Module Definition), CMD (Common Module Definition)

Server: NodeJS Service: CommonJS specification, new version of Node You can also enable the ES6 Module function

Browser side: AMD specification and CMD specification are mainly used, which have been gradually replaced by ES6 Module

Modular specification

1. CommonJS specification

(1) Each file is a module, and each module has an independent scope. The variables in the file, Functions are private and cannot be used in other files (unless assigned to global) (2) Inside each module, the module variable represents the current module (3) The external interface of each file is the module.exports attribute (4) require is used to reference other modules. What is actually obtained is the module.exports attribute of other modules

2. AMD (Asynchromous Module Definition - Asynchronous module definition)

AMD is the standardized output of RequireJS module definition during the promotion process

Use

to define the module define(id?, dependencies?, factory) Load module require([module], callback)

##3. CMD (Common Module Definition - public module definition)

CMD is the standardized output of module definition during the promotion process of SeaJS

Use

to define the module define(factory) Load module require(id)

4. UMD (a blend of AMD and CommonJS)

UMD first determines whether it supports Node.js Whether the module (exports) exists, if it exists, use the Node.js module mode.

When determining whether AMD is supported (whether define exists), if it exists, use the AMD method to load the module.

(function (window, factory) {
    if (typeof exports === 'object') {
     
        module.exports = factory();
    } else if (typeof define === 'function' && define.amd) {
     
        define(factory);
    } else {
     
        window.eventUtil = factory();
    }
})(this, function () {
    //module ...
});
Copy after login

【Related recommendations:

javascript learning tutorial

The above is the detailed content of What are the module specifications of JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template