Skip to content

Instantly share code, notes, and snippets.

@imbcmdth
Created June 20, 2012 06:14
Show Gist options
  • Save imbcmdth/2958401 to your computer and use it in GitHub Desktop.
Save imbcmdth/2958401 to your computer and use it in GitHub Desktop.
Example IIFE
(function(global) { // global is == to window
"use strict"; // Tells JS engine to warn you about silly things you've done
var myVariable = 10;
function internalFunction(val) {
return myVariable * val;
}
global.externalFunction = function(num){
return internalFunction(num);
};
})(window);
// We pass "window" like that above, because we may want to pass other objects
// in future. One possibility is "module" if this library will be used for
// a node.js library or similar.
// Now, in another file later we can do:
var t = externalFunction(2); // t = 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment