!function() { return false; } ()
I’m aware why you might write something like this, but I have a question about the way it works. As I understand it, the exclamation mark does two things:
- It acts on
function() { return false; }, changing it into an expression - It also acts on the result of the executed function, so that the whole line evaluates to
true
So my questions are:
- Is this the correct explanation?
- If it is correct, then since
()binds more tightly than!, how did the first part (the change of the function itself into an expression) happen? Why doesn’t the exclamation mark act on the whole line throughout?
1) It doesnt “change” it. It makes so when the parser goes through the “function” bit it goes expecting an expression, so the “function” is parsed as part of an (possibly anonymous) function expression instead of as a function statement.
2) It is acting on the whole line. If you look at the precedences, as suggested by jbabey, you see that function call binds more tightly then the negation operator so the whole like is evaluated as
Or in a similar, perhaps more readable version: