Home > Article > Web Front-end > Detailed explanation of the specifications and writing formats of the three modules AMD, CMD and UMD
The following three blocks all use foo.js as the example file name, jQuery and underscore as the required components
ADM: Asynchronous module specification, supported format of RequireJs
// 文件名: foo.js define(['jquery', 'underscore'], function ($, _) { // 方法 function a(){}; // 私有方法,因为没有被返回(见下面) function b(){}; // 公共方法,因为被返回了 function c(){}; // 公共方法,因为被返回了 // 暴露公共方法 return { b: b, c: c } });
CommonJs: node Supported formats
// 文件名: foo.js var $ = require('jquery'); var _ = require('underscore'); // methods function a(){}; // 私有方法,因为它没在module.exports中 (见下面) function b(){}; // 公共方法,因为它在module.exports中定义了 function c(){}; // 公共方法,因为它在module.exports中定义了 // 暴露公共方法 module.exports = { b: b, c: c };
UMD: Universal mode, supports the above two formats, and can support the old "global variables" specification
(function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD define(['jquery', 'underscore'], factory); } else if (typeof exports === 'object') { // Node, CommonJS之类的 module.exports = factory(require('jquery'), require('underscore')); } else { // 浏览器全局变量(root 即 window) root.returnExports = factory(root.jQuery, root._); } }(this, function ($, _) { // 方法 function a(){}; // 私有方法,因为它没被返回 (见下面) function b(){}; // 公共方法,因为被返回了 function c(){}; // 公共方法,因为被返回了 // 暴露公共方法 return { b: b, c: c } }));
The above is the detailed content of Detailed explanation of the specifications and writing formats of the three modules AMD, CMD and UMD. For more information, please follow other related articles on the PHP Chinese website!