OSDN Git Service

Regular updates
[twpd/master.git] / umdjs.md
1 ---
2 title: Universal JS module loader
3 category: JavaScript libraries
4 ---
5
6 ### [With dependency](https://github.com/umdjs/umd/blob/master/amdWebGlobal.js)
7
8 ~~~ js
9 ;(function (root, factory) {
10
11   if (typeof define === 'function' && define.amd) {
12     define(['jquery'], factory);
13   } else if (typeof exports === 'object') {
14     module.exports = factory(require('jquery'));
15   } else {
16     root.YourModule = factory(root.jQuery);
17   }
18
19 }(this, function (jquery) {
20   return {};
21 }));
22 ~~~
23
24 ### No dependencies
25
26 ~~~ js
27 ;(function (root, factory) {
28
29   if (typeof define === 'function' && define.amd) {
30     define(factory);
31   } else if (typeof exports === 'object') {
32     module.exports = factory();
33   } else {
34     root.YourModule = factory();
35   }
36
37 }(this, function () {
38   return {};
39 }));
40 ~~~
41
42 ### [Supports circular references](https://github.com/umdjs/umd/blob/master/commonjsStrict.js)
43
44 ~~~ js
45 (function (root, factory) {
46
47   if (typeof define === 'function' && define.amd) {
48     define(['exports', 'jquery'], factory);
49   } else if (typeof exports === 'object') {
50     factory(exports, require('jquery'));
51   } else {
52     factory((root.YourModule = {}), root.jQuery);
53   }
54
55 }(this, function (exports, jQuery) {
56   exports.action = function () {};
57 }));
58 ~~~
59
60 ### Reference
61
62  * https://github.com/umdjs/umd