News for hackers. Stuff that matters.
Re:How to: code JavaScript using Twitter bootstrap best prac... by DrinkWater on Monday May 05, 2014 @07:57AM (#7697997)
"The ! converts the function declaration to a function expression" .... and i closed the tab
Re:How to: code JavaScript using Twitter bootstrap best prac... by cocoflunchy on Monday May 05, 2014 @08:21AM (#7698089)
There's a good discussion over here: https://github.com/airbnb/javascript/issues/44
Re:How to: code JavaScript using Twitter bootstrap best prac... by pjc50 on Monday May 05, 2014 @08:28AM (#7698128)
Oh god. Javascript's optional semicolon really has inflicted a lot of damage, hasn't it?
Re:How to: code JavaScript using Twitter bootstrap best prac... by bshimmin on Monday May 05, 2014 @08:41AM (#7698184)
Horrible, isn't it? Baffling, quasi-random punctuation inserted to prevent random errors caused by poor design choices intended to make programmers' lives easier.
Re:How to: code JavaScript using Twitter bootstrap best prac... by mattmanser on Monday May 05, 2014 @08:35AM (#7698159)
I remember when I used to rave on at my colleagues that it was amazing back in 2005.

How wrong I was.

Re:How to: code JavaScript using Twitter bootstrap best prac... by bpicolo on Monday May 05, 2014 @01:12PM (#7699704)
You lose the return value over (function(){})(). That's an awful recommendation.
Re:How to: code JavaScript using Twitter bootstrap best prac... by _harry on Thursday May 08, 2014 @12:12PM (#7716049)
Yup! We only recommend !function(){}(); for a module where you just need your function to run. If you rely on the return value use (function(){})(); or (function(){}()); (more on this: https://github.com/airbnb/javascript/issues/21)
Re:How to: code JavaScript using Twitter bootstrap best prac... by jbeja on Monday May 05, 2014 @08:12AM (#7698052)
Second to this.
Re:How to: code JavaScript using Twitter bootstrap best prac... by Kiro on Monday May 05, 2014 @08:17AM (#7698076)
Why?
Re:How to: code JavaScript using Twitter bootstrap best prac... by rmrfrmrf on Monday May 05, 2014 @09:15AM (#7698353)
Well, when all is said in done, the statement the author makes is pretty much incorrect in every sense. `!function(){}()` is a type of immediately-invoked function expression. Even if the `!` weren't there, `function(){}` would still be a function expression (compare to `var fun = function(){};`, a function declaration).

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.

Re:How to: code JavaScript using Twitter bootstrap best prac... by wpears on Monday May 05, 2014 @11:30AM (#7699102)
You mixed up your definitions of function declaration and function expression. In this case, if the '!' weren't there, it would indeed be a function declaration, but with improper syntax (removing the '!' would result in a syntax error).

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.

Re:How to: code JavaScript using Twitter bootstrap best prac... by rmrfrmrf on Monday May 05, 2014 @02:00PM (#7700063)
You're right, my mistake. Brain cramp :)
Re:How to: code JavaScript using Twitter bootstrap best prac... by kyllo on Monday May 05, 2014 @12:59PM (#7699616)
I liked one of the responses to the SO thread: http://stackoverflow.com/questions/3755606/what-does-the-exc...

    !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?
Re:How to: code JavaScript using Twitter bootstrap best prac... by BonsaiDen on Monday May 05, 2014 @08:59AM (#7698264)
Prepending a ! results in the parser picking up the line as a expression instead of a statement.

    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.