JS通用模块模式 UMD

历史

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 function add(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

时间: 2024-11-05 22:56:18

JS通用模块模式 UMD的相关文章

js深度模块模式

本文摘自http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html 1 Anonymous Closures (function () { // ... all vars and functions are in this scope only // still maintains access to all globals}()); 2Global Import (function ($, YAHOO) { //

JS模块模式

模块模式是非常常见的模式!它是以下几种模式的组合 命令函数 即时函数 私有和特权成员 声明依赖 该模式的第一步是建立一个命名空间. 下一步是定义该模块,通过即时函数来定义,主要要平衡私有函数和对外接口,同时在即时函数上面,可以声明模块可能有依赖模块的位置.最终结果是一个 由即时函数返回的对象,其中该对象包含了你模块的公共API. MYAPP.utilities.example = (function(){ //依赖模块 var obj = MYAPP.utilities.object; //私有

STM32学习笔记6(TIM通用模块生成PWM)

1.     TIMER输出PWM基本概念   脉冲宽度调制(PWM),是英文“Pulse Width Modulation”的缩写,简称脉宽调制,是利用微处理器的数字输出来对模拟电路进行控制的一种非常有效的技术.简单一点,就是对脉冲宽度的控制.一般用来控制步进电机的速度等等. STM32的定时器除了TIM6和TIM7之外,其他的定时器都可以用来产生PWM输出,其中高级定时器TIM1和TIM8可以同时产生7路的PWM输出,而通用定时器也能同时产生4路的PWM输出. 1.1   PWM输出模式 S

小例子:通用模块——文本框默认提示信息

小例子:通用模块——文本框默认提示信息 因为H5的属性 不兼容所有浏览器 <input type="search" name="" placeholder="提示信息" /> 所以有下面写法: <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> <met

PHP运行模式(cgi,fast-cgi,cli, ISAPI ,web模块模式)【转载】

PHP运行模式有5钟: 1)cgi 通用网关接口(Common Gateway Interface))2)fast-cgi 常驻 (long-live) 型的 CGI3)cli  命令行运行   (Command Line Interface) 4)ISAPI模式(Windows中使用的) 5)web模块模式 (apache等web服务器运行的模块模式) 1.  CGI(Common Gateway Interface) CGI即通用网关接口(Common Gateway Interface),

【javascript设计模式】构造器模式、模块模式、单例模式

构造器模式(Constructor) 构造器模式是学习js最早接触的一种,实现主要如下: 1 function myObject(name){ 2 this.name = name; 3 } 4 5 myObect.propotype = function sayName(){ 6 alert(this.name); 7 } 使用的时候需要new一个对象. 1 var myObj = new myObject('hello'); 2 myObj.sayName(); 模块模式(Module) 模

异步模块模式

简介 众所周知,模块化开是会将复杂的系统分解成高内聚.低耦合的模块,使系统开发变得可控.可维护.可拓展,提高模块复用率.而在js中,异步模块模式的情况则比较多,所谓异步模块模式,是在请求发出后,继续其他业务逻辑,直到模块加载完成后执行后续的逻辑,实现模块开发中对模块加载完成后的引用. 今天就来分析一下异步加载模块,本文通过创建与调度模块.加载模块和设置模块三个方面来分析 创建与调度模块 创建与调度方法集模块创建方法于一身.在这个方法中腰遍历所有依赖模块,并判断所有模块都存在才可执行回调函数,否则

大规模分布式系统架构与设计实战之通用包工头模式

包工头模式 包工头 – 工人模式: 包工头工人模式我是从一本书上看到的<大规模分布式系统架构与设计实战>.“包工头-工人”模式会使用到Anycmd的User模块中去.AC是一个隐式的项目.一个项目只有一个包工头,一个包工头可以雇佣很多工人,一个工人在一个项目下只受雇于一个包工头.每一个工人都是自己的包工头. 这是军队管理的组织结构. 包工头可以把项目拆分成很多子项目,包工头可以把新的子项目承包给新的包工头,每一个工人都可以承包项目,每一个工人都有机会成为包工头,承包项目的工人立即成为该项目的包

【JavaScript】 JS面向对象的模式与实践 (重点整治原型这个熊孩子 (/= _ =)/~┴┴ )

参考书籍 <JavaScript高级语言程序设计>—— Nicholas C.Zakas <你不知道的JavaScript>  —— KYLE SIMPSON 在JS的面向对象编程中,我们最为关注的是两种行为,一是创建对象,二是类继承 JS创建对象 构造函数模式创建对象 第一种创建对象的方式是构造函数模式 如下所示, 将构造函数中的属性和方法赋给一个新对象 /** * description: 构造函数模式创建对象 */ function Type (p) {   this.par