Skip to content

Instantly share code, notes, and snippets.

@cadebward
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cadebward/5b563cc9fadbba502536 to your computer and use it in GitHub Desktop.
Save cadebward/5b563cc9fadbba502536 to your computer and use it in GitHub Desktop.
IIFE
function awesome(param) {
// do something with the param
}
awesome();
(function (window, document) {
// now we have window and document, but on a more local scope
})(window, document);
(function (window, document, undefined) {
// your code
})(window, document);
(function () {
// do something
})();
(function (param) {
// do something with the param
})(param)
(function ($, window, document, undefined) {
// now we have $, window, document, and undefined
})(jQuery, window, document);
(function () {
var name = 'cade';
})();
console.log(name); // undefined
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory;
} else {
root.MYMODULE = factory(root);
}
})(this, function (root) {
// code here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment