历史

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

1 //math.js

2 export functionadd(a, b) {3 return a +b;4 }5

6 //app.js:指定使用math模块的add命名导出

7 import { add } from ‘./math.js‘;8 console.log(add(1, 2)); //=> 3

9

10 //导入所有的命名导出作为math对象的成员

11 import * as math from ‘./math.js‘;12 console.log(math.add(1, 2)); //=> 3

原文:https://www.cnblogs.com/lightsong/p/10353278.html

html5读取umd,JS通用模块模式 UMD相关推荐

  1. AMD,CMD,UMD 三种模块规范 写法格式

    一下三块均以 foo.js 为示例文件名,以 jQuery,underscore 为需求组件 ADM:异步模块规范, RequireJs 的支持格式 1 // 文件名: foo.js 2 define ...

  2. webpack 通用模块(每个页面都用到的js)编译

    1.项目目录 2.配置文件:webpack.config.js var htmlWebpackPlugin = require('html-webpack-plugin'); var webpack ...

  3. JS 设计模式四 -- 模块模式

    概念 模块模式的思路 就是 就是单例模式添加私有属性和私有方法,减少全局变量的使用. 简单的代码结构: var singleMode = (function(){// 创建私有变量var privat ...

  4. [Js代码风格]浅析模块模式

    1.实例解释模块模式 简明扼要的说,经典的模块模式指的定义一个立即执行的匿名函数.在函数中定义私有函数和私有变量并且返回一个包含公共变量和公共函数作为属性和方法的匿名对象. var classicMo ...

  5. JavaScript之后端Web服务器开发Node.JS基本模块学习篇

    JavaScript之后端Web服务器开发Node.JS基本模块学习篇 基本模块 fs文件系统模块 stream支持流模块 http crypto加密模块 基本模块 因为Node.js是运行在服务区端 ...

  6. JS模块化——模块暴露与模块引入

    1. 引言 最近在研究前端框架,但发现好多JavaScript知识不是很了解,很是苦恼,下面就来研究一下JavaScript的模块化,先理解几个概念和模块化的进化过程. 2. 模块化 2.1 什么是模 ...

  7. javascript模块模式深度探索 豆瓣javascript组译文2

    前言 模块模式是基于js闭包实现的一个模式,这篇文章描述如何用模块模式来支持多人大型项目,此外,需要自己做框架的同学也可以参考. -煎蛋 模块模式深度探索模块模式是一个常用的js编程模式.它很好理解, ...

  8. node.js中模块_在Node.js中需要模块:您需要知道的一切

    node.js中模块 by Samer Buna 通过Samer Buna 在Node.js中需要模块:您需要知道的一切 (Requiring modules in Node.js: Everythi ...

  9. Node.js Web 模块

    Node.js Web 模块 什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服务器的基本功能就是提供Web信息浏览服务.它只需支持HTTP协议. ...

最新文章

  1. 系统异常设计规范与原则
  2. Vivado 随笔(5) 行为仿真(Behavior Simulation)相关事宜?
  3. Head First JSP---随笔十(过滤器的威力)
  4. Linux系列:Ubuntu/fedora实用小技巧—禁止自动锁屏、设置免密码自动登录、免密码执行sudo操作...
  5. Linux下使用socket传输文件的C语言简单实现
  6. python-函数的嵌套调用
  7. Python《爬虫初实践》
  8. Python+matplotlib数据可视化鼠标悬停自动标注功能实现
  9. php redis 里面的hscan 第四个参数count很不靠谱
  10. word文件退出只读模式
  11. 说话没技巧,母猪都嫌吵!
  12. 视频在线播放,边下边播的一些问题记录
  13. 英伟达RTX 4070最新测评来了!光追效果更棒,但仅限于2k游戏
  14. 【云速建站如何个人备案】
  15. 高博SLAM基础课第四讲——非线性优化
  16. 宇信易诚 为何成长如此之快
  17. SOM神经网络图像分类tensorflow实现
  18. 电阻、电容贴片封装的定义
  19. 好用的桌面便签工具-Microsoft便笺
  20. mac升级10.15 360命令行加固脚本报错解决

热门文章

  1. 图片使用内存法进行浮雕处理_无锡浮雕景观雕塑制作安装
  2. matlab bvp4c猜测解,对具有两个解的 BVP 求解
  3. 个人站点网页设计html,响应式网页设计的快速教程(适合个人站点)
  4. 百度离线地图WMS/WMTS服务
  5. AutoCAD 2020快捷指令大全
  6. miflash刷机出现cannot receive hello packet,和一开始认识9008端口之后不认问题。
  7. WorkNC配置与MAKINO牧野 MCC2013 6轴加工中心
  8. 2018.11.28——DBSCAN用于聚类、异常点检测
  9. PS素材挖掘七式(留学 个人陈述 personal statement)
  10. python 关于新型冠状病毒代码!