!function() {
// Code in here is encapsulated
}();
Note that !
or +
is safer than ()
because ()
could invoke preceding code.
// Parens invoke the console.log
console.log
(function() {
return "invoked"
}());
// Won't log anything
console.log
!function() {
return "invoked"
}();
// Won't log anything
console.log
+function() {
return "invoked"
}();
!
seems ideal because it like cautions that the code ahead is a closure
unrelated gist image experiment...this image fit the mood =p