3

I saw in the ParsleyJs library the folowing:

enter image description here

What does the expression !(function(f){...}) mean?
Is it a negation?

EDIT:
After some explanations, I observed that actually the code looks like

!( f(y){}( f(x){} ) );

or can be written as

!( f(z) );

or

!(Z);

where Z = f(z), z = f(y){}, and finally y = f(x){}...
So is not really clear what function executes the expression !(Z);

serhio
  • 28,010
  • 62
  • 221
  • 374
  • 4
    I suspect that it means the programmer was superstitious. It's completely unnecessary in the posted code. – Pointy Apr 01 '15 at 14:31
  • 1
    Related: http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function – Andrew Whitaker Apr 01 '15 at 14:31
  • 1
    Possible duplicate [http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function](http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function) – DrKey Apr 01 '15 at 14:31
  • 1
    It's pointless as the IIFE is already wrapped in parenthesis. – mbx-mbx Apr 01 '15 at 14:33
  • 1
    Well - it look's like this actually `!(function(f){...} (function ($){...}));` It doesn't look like anything is actually ever called – Adjit Apr 01 '15 at 14:34
  • actually there is `!( f(y){}( f(x){} ) );` or can be written as **`!( f(z) );`** where `z = f(y){}`, where `y = f(x){}`, so not really clear what for is the `!(Z);` – serhio Apr 01 '15 at 14:57
  • @Pointy I think you are wrong: actually there is `!(f(z));` that is an expression. Without will be `(f(z));` - perhaps not the same thing.... – serhio Apr 01 '15 at 15:27
  • 1
    No, it's the same thing. – Pointy Apr 01 '15 at 15:29

2 Answers2

4

It is a short-hand or alternative for self-invoking anonymous function.

(function(){
//code
})();`

can be written as

!function(){
// code
}();

you can also use + instead of !.

Amandeep Singh
  • 320
  • 2
  • 9
  • actually there is `!( f(y){}( f(x){} ) );` or can be written as **`!( f(z) );`** where `z = f(y){}`, where `y = f(x){}`, so not really clear what for is the `!(z);` – serhio Apr 01 '15 at 14:59
2

Usually you use either

!function(f){...}()

or

(function(f){...})()

or

+function(f){...}()

The developers here combined the first two, which is redundant.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • Beware of the last once since `+` is overloaded as a binary operator, so it has the same potential issues as `()` or any binary/postfix operator. Unary prefix operators are best for this. In the past, I've used `void` since it stands out. –  Apr 01 '15 at 14:37
  • Stolen from the links in the comments: Another benefit is that ! causes a semi-colon insertion, so it's impossible for this version to be wrongly concatenated with a file that doesn't end with a ; – mbx-mbx Apr 01 '15 at 14:37
  • ...and this doesn't really answer the question. –  Apr 01 '15 at 14:40
  • actually there is `!( f(y){}( f(x){} ) );` or can be written as **`!( f(z) );`** where `z = f(y){}`, where `y = f(x){}`, so not really clear what for is the `!(z);` – serhio Apr 01 '15 at 14:56