Created
June 20, 2012 06:14
-
-
Save imbcmdth/2958401 to your computer and use it in GitHub Desktop.
Example IIFE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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