Home  >  Article  >  Web Front-end  >  In-depth understanding of the concepts and usage of commonJS

In-depth understanding of the concepts and usage of commonJS

零到壹度
零到壹度Original
2018-04-12 15:56:533940browse

The content of this article is to share with you an in-depth understanding of the concept and usage of commonJS. It has a certain reference value. Friends in need can refer to it

##Common Understanding of .js!

commonJS specification:

1. The file is the module
Each file is a scope, and the variables\functions defined in the file are Private,

is invisible to other modules;

2. Use module.exports (exports) to expose external interfaces.
The module variable represents the current module, Module is an object. Use the exports attribute of this object to expose the external interface;

3. Use require to load dependent modules synchronously
For example:


a. js: This is an immediately executed function expression to expose the interface

var moduleA = (function () {
var aFlag = false;
function keepMoving () {
}
b = 1;
// 只暴露keepMoving 方法
return {
keepMoving: keepMoving
};
})()


a.js The code of function module a

var aFlag = false;
function keepMoving () {
}
b = 1;
module.exports = {//使用 module.exports 暴露接口
keepMoving: keepMoving
};


d.js This module depends on the above Module A

//使用require加载模块A;
var moduleA = require('./a.js');
function methodD() {//调用模块a里面的方法
moduleA.keepMoving();
}
//暴露模块D的接口
module.exports = {
methodD: methodD
};

Related recommendations:

First introduction to commonjs

Brief understanding of CommonJS specifications

##javascript modularization of CommonJS, AMD, CMD, UMD, ES6

The above is the detailed content of In-depth understanding of the concepts and usage of commonJS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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