Skip to content

Instantly share code, notes, and snippets.

@tekaratzas
Last active July 24, 2017 13:56
Show Gist options
  • Save tekaratzas/bdf3e4619dc5297405658225a96b228a to your computer and use it in GitHub Desktop.
Save tekaratzas/bdf3e4619dc5297405658225a96b228a to your computer and use it in GitHub Desktop.
Anonymous Wrappers and Namespaces
(
// evaluate the function in the parenthesis
function(){}
// return the evaluated function object
)() // call it immediately
// The same functionality can be done as follows;
!function(){}()
+function(){}()
(function(){}());
// PRO TIP:
// if some jerk on your team does this. AKA overwrites the global variable "undefined"
console.log(typeof undefined === "undefined") // true
var undefined = 123;
console.log(typeof undefined === "undefined") // this is false! undefined is overwritten and replaced by the number 123
// you can avoid a catastrophe by using an anonymous wrapper.
(function(undefined){console.log(undefined);})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment