Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of such definition?

While looking via some modules sources for node.js I have found one having the following construction:

;(function(global){

    var a = // some definitions
    .. // code



})(typeof window === "object" ? window : this);

So what is the meaning to write:

  1. semicolon in front of code?
  2. to write (typeof window === "object" ? window : this); ?
like image 752
kaytrance Avatar asked Dec 13 '25 11:12

kaytrance


1 Answers

The semicolon is a safety measure for minifications (elaborated on here).

The second part of your question: (typeof window === "object" ? window : this) is checking whether the code runs in a browser. If window is actually defined, then we conclude it runs in a browser, if not it runs in node. Then we pass this environment (node.js or window) as a variable.

like image 123
MeLight Avatar answered Dec 15 '25 07:12

MeLight