JS Module specification

Source: Internet
Author: User

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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.