COMMONJS is used in server-side, synchronous, such as Nodejs; Amd,cmd is used on the browser side, AMD is asynchronous module loaded, such as Requirejs;cmd is a generic module loaded, such as SEAJS. Among them, AMD first proposed, CMD is based on COMMONJS and AMD based on the proposed. COMMONJSCOMMONJS is a specification for server-side modules, and node. JS uses this specification. According to the
1, Commonjs is a kind of specification, NODEJS is the realization of this kind of norm.1.1, CommonJS loading module is synchronous, so only the loading is complete to perform the subsequent operation.2, AMD is REQUIREJS in the promotion process of the module definition of standardized output.2.1, AMD asynchronous loading module.3, CMD is seajs in the promotion process of the module definition of standardize
interfaces of these modules, typical examples such as the YUI library.This primitive loading method exposes some obvious drawbacks:
Variable collisions can be caused by global scope
Files can only be loaded in the order in which they are written
Developers must subjectively resolve dependencies between modules and code base
Resources are difficult to manage in large-scale projects, and long-term accumulation of problems leads to a confusing code base
CommonJSThe serve
AMD, CMD, COMMONJS specificationThe idea of splitting the JS code into small chunks with different functions is popular in some tripartite specifications, such as COMMONJS, AMD, and CMD. Let's take a look at these kinds of specifications.
I. Modular specificationCommonjs is used on the server side, is synchronous loading, Nodejs is the practice of this specification.Amd,cmd is used on the browser side
Why Modular programmingIf JS can also be like Python,java using import, the introduction of the module we want, what module we want to load what module, can give the front-end programming to bring more convenient, more clear structure. However, there is a premise that you must write the module in the same way.Currently, there are two types of JavaScript module specifications: Commonjs and AMD.CommonJSIn 2009, US programmer Ryan Dahl created the node.
The use of modular programming at work is also a period of time, mainly used in AMD and COMMONJS two styles of modular programming. Modular programming compared to the traditional JS programming style, the traditional JS default is not similar to the Java Class,package Concept, reference to multiple JS prone to pollution global variables, membership coverage and the problem of dependency chaos, in order to avoid such problems, only to derive the modul
With the development of JS Modular programming, the dependence between the processing modules becomes the key to maintain.ModularAmd,cmd,commonjs is currently the most commonly used three modular writing specifications.CommonJSThe COMMONJS specification was born earlier. Nodejs adopted the COMMONJS. This is how the module is loaded:var clock = require(‘clock‘);cl
With the development of JS Modular programming, the dependence between the processing modules becomes the key to maintain.Amd,cmd,commonjs is currently the most commonly used three modular writing specifications.CommonJSThe COMMONJS specification was born earlier. node. JS (which is a JavaScript runtime) uses COMMONJS. This is how the module is loaded:Foo.jsmodul
From:https://www.jianshu.com/p/09ffac7a3b2cWith the development of JS Modular programming, the dependence between the processing modules becomes the key to maintain.ModularAmd,cmd,commonjs is currently the most commonly used three modular writing specifications.CommonJSThe COMMONJS specification was born earlier. Nodejs adopted the COMMONJS. This is how the modul
1, overviewCOMMONJS is a specification for server-side modules, and node. JS uses this specification.According to the COMMONJS specification, a single file is a module. The load module uses the Require method, which reads a file and executes it, and finally returns the exports object inside the file. The following is a simple module file example.js.Console.log ("Evaluating Example.js"); var function () { console.log ("invisible"= "HI"function () {
1. What is ModularFile scopeCommunication rulesLoad requireExport exports2, COMMONJS module specificationJavaScript in node also has a very important concept: module conceptModule scopeUse the Require method to load a moduleSyntax: var custom variable name = require (' module ')Two functions:Executing the code in the loaded moduleGet the exports export interface object in the loaded moduleUse the exports excuse object to export members in a moduleThe
Commonjs/nodejs implementation
Variables provided by the node. JS context
Require: a function that refers to a dependent module, the dependent module is referenced as an object, can consume the provided methods and variables, and require () accepts a module identification parameter.
Module: Object that represents the current module itself
Exports: Object, module's properties, as the only export of modules, used to expor
The modular development method can improve the code reuse rate and facilitate the management of code. Usually a file is a module that has its own scope, exposing only specific variables and functions to the outside. The current popular JS Modular specification has COMMONJS, AMD, cmd and ES6 module system. The evolution of the front-end modularity can be seen in the Nanyi teacher's article. This paper mainly introduces the modular realization mode unde
This article is referenced from: https://www.cnblogs.com/chenguangliang/p/5856701.html1, CommonJSNodejs is the implementation of COMMONJS specification, Webpack is also in the form of Commonjs to write. node. JS uses the JavaScript language for server-side programming.2. AMDBased on the COMMONJS specification of the Nodejs out, the service-side module concept has
As is known to all, Package.json is a document written by Commonjs to describe a narrative package, and a fully compliant Package.json file should contain a field.Name: The names of the packages must be unique. Consists of lowercase English letters, numbers, and underscores. Cannot include spaces.Description: A brief description of the package.Version: A string that conforms to the revision number of the semantic eight identification specification.Key
Modularity of the CommonjsThe node application consists of modules, each with its own scope, and functions, variables, classes, and so on are private.COMMONJS specifies that within each module, the module variable (which is an object) represents the current module and module.exports is an external interface. Other files require are loaded by means of this interface to obtain the data that the module outputs.Features of the COMMONJS module:
Al
AMD (Asynchronous module definition) is the normalized output defined by Requirejs in the promotion process.function (Lib) { function foo () { lib.log (' Hello world! ') ); } return { foo:foo };});CMD (Synchronous module definition) is a normalized output defined by SEAJS in the process of generalization.// all modules are defined by define. define (function(require,exports,module) { // by require introduction of dependency var $ = require (' jquery '); var Spinning = re
Commonjs module specification
The modular specification of commonjs is described inModules/1.1.1Medium
Currently, the following packages are available:
Yabble, couchdb, narwhal(0.2 ),Wakanda,Teajs (formerly v8cgi ),Commonscript,Pinf JS loader,Seajs,Arangodb,Sorrow. js
Note that requirejs is not found here because it uses amd specifications.
The specific content defined in this Specification includes:
Re
Commonjs and AMD essentially define a namespace with a global variableTake Sea.js as an example, each module outputs an object, and the object is mounted under the Seajs.cache property, and each module exists as an object.While the ES6 module is not, the module object does not exist when the module is not referenced.Commonjs and AMD are run-time loaded, ES6 modules are loaded at compile timeThis distinction can explain the difference, which is the mos
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.