How to: code JavaScript using Twitter bootstrap best practices
theodo.fr
theodo.fr
(function() {
//code here
})();
This won't affect the return type of the statement; which in case of using ! will return boolean 'true' unless the function returns a truthy value. Plus using ! for this purpose, looks totally messy. (function($,window) {
//code here
})(jQuery,window); (function(window, $, otherGlobal) {
$ = window.jQuery;
otherGlobal = window.otherGlobal;
//code here
})(window); (function(){/* stuff */}())
(function(){/* other stuff */}())
Is interpreted as: (function(){/* stuff */)())(function(){/* other stuff */}())
To correct this, you also need a semicolon. ;(function() {
//code here
})();
Alternatively, if you can stomach it (I can't), you can use the !. function foo(){}; // statement
var a = function() {}; // the right side of the = is a expression, a function keyword inside a expression does also not require a name
Also function statements are put into the scope before execution of the program begins, expressions on the other hand need to be evaluated.So this will work:
foo();
function foo(){}
While this will throw the usual `Undefined is not a function` (it does not throw foo is not defined, because the var statement gets hoisted, meaning before the code is run the compiler will move all var statements to the top of the nearest scope (i.e. function) foo();
var foo = function(){};
Now, as for why he closed the tab, first of all it is pretty clear that this is a very dumbed down explanation of the whole thing, and while there is a SO link it's not immediately clear that all operator tokens can trigger this behavior.Second, `!` in JavaScript does not convert a function declaration into a function expression; it invokes the NOT operation on whatever immediately follows it.
Examples:
> !true;
false
> !false;
true
> !4;
false
> !undefined;
true
> !!undefined;
false
So, basically what this code is doing is creating a function, calling it (with `()`), and then running a NOT operation on it.I personally hate this kind of code because it's hacky and works by exploiting the interpreter. If you look at the examples of the `!` operator I gave, `!undefined` returns `true`, which is exactly what the author's code will return. Granted, the chances that you'll get bitten for using `!` over just a pair of parens is slim, you're still forcing the interpreter to waste [an incredibly small amount of] time performing useless operations just because the programmer was too lazy to type parentheses.
Much better, IMO, would be `(function() {})()` or, more safely and explicitly, `(function() {}).call(this)`, which sets the function expression's context to Window even in strict mode. `foo()` is syntactic sugar for `foo.call(undefined)` in strict mode and `foo.call(window)` in non-strict mode.
What makes a function either an expression or declaration is simply whether the parser expects an expression or declaration in the given context. The grouping operator '()' and the NOT operator '!' both are parameterized by expressions, and so the Javascript parser treats functions passed to them as expressions. Unless located where an expression is expected, functions will default to declarations.
!function () {}()
This will also return the boolean opposite of the return value of the function, in this case true, because !undefined is true. If you want the actual return value to be the result of the call, then try doing it this way: (function () {})()
Why would you ever not want the actual return value of a function to be the result of calling that function?How wrong I was.