Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Export your awesome module using AMD, CommonJS, Node.js or just as global.
/*
* Inspiration (well… copy pasting more or less) from:
* https://github.com/ScottHamper/Cookies/blob/0.3.1/src/cookies.js#L127-L140
*
* Thanks Scott!
*/
(function (global) {
'use strict';
var MyModule = function () {
/* Your awesome module logic here… */
};
/* …and here */
MyModule.foo = 'bar';
// AMD support
if (typeof define === 'function' && define.amd) {
define(function () { return MyModule; });
// CommonJS and Node.js module support.
} else if (typeof exports !== 'undefined') {
// Support Node.js specific `module.exports` (which can be a function)
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = MyModule;
}
// But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
exports.MyModule = MyModule;
} else {
global.MyModule = MyModule;
}
})(this);
/*
* Inspiration from:
* https://github.com/jquery/jquery/blob/master/src/intro.js
*
* Thanks jQuery!
*
* This is another way to handle dependencies (in this case, a window with a document)
*/
(function( global, factory ) {
if ( typeof module === "object" && typeof module.exports === "object" ) {
// For CommonJS and CommonJS-like environments where a proper window is present,
// execute the factory and get jQuery
// For environments that do not inherently posses a window with a document
// (such as Node.js), expose a jQuery-making factory as module.exports
module.exports = global.document ?
factory( global, true ) :
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}
// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
// Awesome module gets created here
}));
/*
* Inspiration from:
* https://github.com/addyosmani/memoize.js/blob/master/memoize.js
*
* Thanks Addy!
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.memoize = factory();
}
}(this, function() {
"use strict";
var memoize = function(func) {
/* Your awesome module logic here */
};
return memoize;
}));
@YanLIU0822

This comment has been minimized.

Copy link

YanLIU0822 commented Aug 21, 2017

Hey I am going through the jquery 3.2.1, find the first part of this, but coz i am not familiar with module, i cant figure out this part is for. would you mind give me a little explanation? thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.