转载请注明出处: http://qiudeqing.com/javascript/2015/08/09/umd.html

同时支持CommonJs规范, AMD规范, 全局加载的模块, 详细介绍可以参看umd项目, what-is-amd-commonjs-and-umd

普通UMD模板

;(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
}));

jQuery插件UMD模板

;(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.CityPicker = factory(root.jQuery)
    }
}(this, function ($) {

    var dataKey = 'citypicker.qiu'

    function CityPicker() {}
    CityPicker.prototype = {
        reset: $.noop
    }

    function Plugin() {
        return this.each(function (option, city) {
            var $this = $(this)
            var citypicker = $this.data(dataKey)
            var options = typeof option === 'object' && option

            if (!citypicker) {
                $this.data(dataKey, (citypicker = new CityPicker(this, options)))
            }

            if (option === 'reset') {
                CityPicker.reset()
            }
        })
    }

    var old = $.fn.citypicker
    $.fn.citypicker = Plugin
    $.fn.citypicker.Constructor = CityPicker
    $.fn.citypicker.noConflict = function () {
        $.fn.citypicker = old
        return this
    }

    //    exposed constructor
    return CityPicker
}));

参考资料