1 //COMMONJS module Specification,2 //Each module has the require, exports, module 3 variables, Nodejs to get the content of the JS file is wrapped in the tail3(function(exports, Require, module, __filename, __dirname) {4 varMath = require (' Math '); 5Exports.area =function(RADIUS) {6 returnMath.PI * RADIUS *radius;7 }; 8 }); 9 Ten //the AMD (asynchronous Module Definition) specification is an extension of the COMMONJS specification, One //Https://github.com/amdjs/amdjs-api/wiki/AMD A //The module defines the following define (ID, dependencies, Factory); its module ID and dependencies are optional, - //The first parameter ID is a string type that represents the module ID, which is an optional parameter. If it does not exist, the module identity should be defined by default as the identity of the requested script in the loader. If present, then the module ID must be a top-level or an absolute identity. - //The second parameter, dependencies, is an array literal that the current module relies on to identify the module that has been defined by the module. the //The third argument, factory, is a function or an object that needs to be instantiated. - - //create modules identified as alpha, dependent on require, export, and modules identified as Beta -Define ("Alpha", ["require", "exports", "beta"],function(Require, exports, beta) { +Export.verb =function(){ - returnBeta.verb (); + //or: A returnRequire ("beta"). verb (); at } - }); - - //an asynchronous module that returns the literal of an object -define (["Alpha"],function(Alpha) { - return { inVerb:function(){ - returnAlpha.verb () + 1 ; to } + } - }); the * //no-dependency modules can be defined directly using object literals $ define ({Panax NotoginsengAdd:function(x, y) { - returnX +y; the } + } ); A the //similar to the CommonJS method definition +Definefunction(Require, exports, module) { - varA = require (' a '), $b = require (' B '); $ -Exports.action =function(){}; - } ); the -Definefunction() { Wuyi varExports = {}; theExports.sayhello =function() { -Alert (' Hello from module: ' +module.id); Wu }; - returnexports; About }); $ -define ([' Dep1 ', ' Dep2 '),function(DEP1, DEP2) { - return function () {}; - }); A + the //CMD (Common Module Definition) Specification -Definefunction(Require, exports, module) { $ //The module code goes here the }); the the compatible with multiple module specifications the;(function(name, definition) { - //detects if the context is AMD or CMD in varHasdefine =typeofdefine = = = ' function ', the //Check if the context is a node theHasexports =typeofModule!== ' undefined ' &&Module.exports; About the if(hasdefine) { the //AMD Environment or CMD environment the define (definition); +}Else if(hasexports) { - //defined as node module theModule.exports =definition ();Bayi}Else { the //hangs the execution result of the module in the window variable, which in the browser points to the Window object the This[Name] =definition (); - } -}) (' Hello ',function () { the varHello =function () {}; the returnHello; the});
JS Module specification