历史

JS诞生之初面向简单页面开发, 没有模块的概念。

后来页面逐渐复杂, 人类构造到 IIFE 立即执行函数来模拟 模块;

之前也有雅虎的实践,使用命名空间 作为模块名。

最后衍生出 面向各种使用场景 的 JS 模块标准。

例如:

面向浏览器的 AMD

面向Nodejs的 CommonJS

对于这种分裂状态ES标准也在尽力弥合。 但是目前流行的实践是 UMD模式。

AMD

https://www.davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/

Asynchronous Module Definition (AMD) has gained traction on the frontend, with RequireJS being the most popular implementation.

Here’s module foo with a single dependency on jquery:

//    filename: foo.js
define(['jquery'], function ($) {
// methods
function myFunc(){}; // exposed public methods
return myFunc;
});

And a little more complicated example with multiple dependencies and multiple exposed methods:

//    filename: foo.js
define(['jquery', 'underscore'], function ($, _) {
// methods
function a(){}; // private because it's not returned (see below)
function b(){}; // public because it's returned
function c(){}; // public because it's returned // exposed public methods
return {
b: b,
c: c
}
});

CommonJS

CommonJS is a style you may be familiar with if you’re written anything in Node (which uses a slight variant). It’s also been gaining traction on the frontend with Browserify.

Using the same format as before, here’s what our foo module looks like in CommonJS:

//    filename: foo.js

//    dependencies
var $ = require('jquery'); // methods
function myFunc(){}; // exposed public method (single)
module.exports = myFunc;

And our more complicate example, with multiple dependencies and multiple exposed methods:

//    filename: foo.js
var $ = require('jquery');
var _ = require('underscore'); // methods
function a(){}; // private because it's omitted from module.exports (see below)
function b(){}; // public because it's defined in module.exports
function c(){}; // public because it's defined in module.exports // exposed public methods
module.exports = {
b: b,
c: c
};

兼容模式UMD

Since CommonJS and AMD styles have both been equally popular, it seems there’s yet no consensus. This has brought about the push for a “universal” pattern that supports both styles, which brings us to none other than the Universal Module Definition.

The pattern is admittedly ugly, but is both AMD and CommonJS compatible, as well as supporting the old-style “global” variable definition:

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory(require('jquery'));
} else {
// Browser globals (root is window)
root.returnExports = factory(root.jQuery);
}
}(this, function ($) {
// methods
function myFunc(){}; // exposed public method
return myFunc;
}));

And keeping in the same pattern as the above examples, the more complicated case with multiple dependencies and multiple exposed methods:

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery', 'underscore'], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory(require('jquery'), require('underscore'));
} else {
// Browser globals (root is window)
root.returnExports = factory(root.jQuery, root._);
}
}(this, function ($, _) {
// methods
function a(){}; // private because it's not returned (see below)
function b(){}; // public because it's returned
function c(){}; // public because it's returned // exposed public methods
return {
b: b,
c: c
}
}));

(Sep 2014 edit: fixed syntax for CommonJS in the last example)

官网

https://github.com/umdjs/umd

This repository formalizes the design and implementation of the Universal Module Definition (UMD) API for JavaScript modules. These are modules which are capable of working everywhere, be it in the client, on the server or elsewhere.

The UMD pattern typically attempts to offer compatibility with the most popular script loaders of the day (e.g RequireJS amongst others). In many cases it uses AMD as a base, with special-casing added to handle CommonJS compatibility.

Variations

Regular Module

  • amdWeb.js - Defines a module that works with AMD and browser globals. If you also want to export a global even when AMD is in play (useful if you are loading other scripts that still expect that global), use amdWebGlobal.js.
  • returnExports.js - Defines a module that works in Node, AMD and browser globals. If you also want to export a global even when AMD is in play (useful if you are loading other scripts that still expect that global), use returnExportsGlobal.js.
  • commonjsStrict.js - Defines a module that works with more CommonJS runtimes, and for modules that will have a circular dependency. If you also want to export a global even when AMD is in play (useful if you are loading other scripts that still expect that global), use commonjsStrictGlobal.js

jQuery Plugin

  • jqueryPlugin.js - Defines a jQuery plugin that works with AMD and browser globals.

ES6模块

https://www.cnblogs.com/polk6/p/js-ES6-module.html

 // math.js
export function add(a, b) {
return a + b;
} // app.js:指定使用math模块的add命名导出
import { add } from './math.js';
console.log(add(1, 2)); // => 3 // 导入所有的命名导出作为math对象的成员
import * as math from './math.js';
console.log(math.add(1, 2)); // => 3

最新文章

  1. 419. Battleships in a Board
  2. ASP.NET Core中的依赖注入(5): ServiceProvider实现揭秘 【解读ServiceCallSite 】
  3. Expression: is_block_type_valid(header->block_use)
  4. 判断IE版本的HTML语句详解,如:[if lte IE 9]……[endif]
  5. poj1743 后缀数组求不可重叠的重复出现的子串最长长度
  6. C# 词法分析器(六)构造词法分析器
  7. 【MySQL】MySQL快速插入大量数据
  8. 利用 NSSortDescriptor 对 NSMutableArray 排序
  9. Python快速建站系列-Part.One-组装开发环境
  10. 【IHttpHandler】ASP.NET 生命周期
  11. mac terminal终端ls命令参数详解
  12. 圣诞福利到!51Testing邀你一起来狂欢!有礼就是任性~(≧▽≦)/~
  13. Win7/Win8右键菜单管理工具(Easy Context Menu) v1.5 绿色版
  14. 为linux系统实现回收站
  15. ECMAScript迭代语句
  16. JAVA课程设计+五子棋游戏
  17. 出现Unreachable code问题的原因
  18. a标签传递参数
  19. java网络爬虫基础学习(三)
  20. asp.netajax与jquery和bootstrap的无刷新完美实现

热门文章

  1. docker根据配置文件启动redis
  2. Eclipse里JAR文件的打包和使用
  3. requests库下载图片的方法
  4. Arch LInux安装dde(Deepin Desktop Environment 深度桌面环境)
  5. react-router(v4)
  6. Runnable和Callable之间的区别
  7. SQL操作符、通配符等
  8. python定时执行任务的三种方式
  9. 微信小程序-表单笔记2
  10. 认识 CXF(WebService框架)