0

I was reading through some javascript code, and encountered a rather strange notation of a function declaration.

!function(t) {
  ... lots of code ...
}("undefined" == typeof exports ? d3.layout || (d3.layout = {}) : exports);

And it was never called anywhere in the code. However, the function is executing. I have tried evaluating !function(){return true;}, !function(){return false;}, and several other return values, however I have not been able to determine what it is actually doing. (The expression just evaluates to false).

What is the !function notation, and when/how should it be used?

Chris Britt
  • 1,443
  • 1
  • 17
  • 35
  • *"it was never called anywhere in the code"* It's called right there! The `("undefined" == typeof exports ? d3.layout || (d3.layout = {}) : exports)` part is what calls the function with a single argument. – Felix Kling May 09 '17 at 20:32
  • What do you mean, it's never called anywhere? It's called right there, where it's declared. The term is IIFE (immediately invoked function expression) – Sergio Tulentsev May 09 '17 at 20:32
  • `theResult = NOT(result of running IIFE);` – James May 09 '17 at 20:33

0 Answers0