Node-CommonJS specification, module scope

Modular


In order to resolve dependencies between files, modular specification is a good agreement.
The modular specification is a plain text of the agreement, the developers have to write code to the specifications, reduce communication costs, a great convenience to call each other between the modules.

First, understand CommonJS specification

  1. Action: is a Javascript modular specification, the predetermined characteristics of the module and how the interdependence between the modules ;

  2. Uses: Node.js CommonJS specifications are used;

  3. Features: Synchronous load module , not suitable for use in the browser; browser using the AMD / CMD.

  4. CommonJS specification defines what: Wiki description for Modules

4.1 如何引入: In a module, there is a free variable "require", that is a Function.
4.2 如何暴露给别人使用:In a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.
4.3 必须有一个变量module :In a module, there must be a free variable "module", that is an Object.

Second, the browser AMD and CMD modular specification

Note: You can not use CommonJS browser specifications; for at CommonJS, synchronization module is loaded;

AMD / CMD may be understood to be in the browser commonjs solutions, AMD / CMD, modules are loaded asynchronously;

1. AMD representatives modular specification: RequireJS

  • Key Features 1: For dependent modules, AMD is executed in advance;
  • Main characteristics 2: Pre-dependent respected;

2. CMD on behalf of modular specifications: SeaJS

  • 1 Main characteristics: dependency on the module, CMD delay is performed; the CMD respected as lazy as possible.
  • 2 Main characteristics: dependency nearest respected;

3. ES6 modular (trend): ES6 in language standard level, to achieve a module function, but also achieved quite simple, and can replace CommonJS AMD specification, become the generic browser and server module solutions;

Third, the module scope and global scope

There are two scopes Node.js, are global scope and the module scope ;

  1. Global scope: the use of global access, similar to the browser window ;
  2. Each Javascript file is a separate module, each module has its own independent scope, therefore: the module members, the default can not be accessed by other modules.
  3. Default Node, the method defined in each of the JS file, variables belong module scope.

Mount to global (not recommended, variable names pollution)

 

var b = 20;
global.b = b; 
global.say = function () {
  console.log('这是挂载到全局的 say 方法');
}

Fourth, the module scope

1. module (module identification)

Common JS module attributes are defined in the specification, it is an object that represents the current js this particular module;

2. require (reference module)

Each module implements CommonJS specification must define a require () function, require the use of this function, it can be easily introduced into the members of the other modules, for its own use;

3. exports (exposure module members)

Each module, if you want to put some of their own private members exposed to others to use, then, must implement an exports target by exports objects, you can easily put the private members within the module, exposed to the outside world use;

Ties between the Five, module.exports and exports of

  1. module.exports and exports refer to the same default an empty object;
  2. module.exports exports and consistent action, members are outwardly exposed;
  3. A module scope, when private members exposed outside, always with module.exports prevail;



Author: zhouhao_180
link: https: //www.jianshu.com/p/6fb38b916cd9
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Released four original articles · won praise 0 · Views 121

Guess you like

Origin blog.csdn.net/xiaokanfuchen86/article/details/104480395