1.

Explain Generators in JavaScript?

Answer»

The leading bang! Is USED in anonymous functions:

!FUNCTION(){   // }();

With that, you can also write it as:

+function(){   // }()

Generally, the leading bang (semi-colon) is used in functions in libraries. Let us see the DIFFERENT uses of including a leading semicolon:

Appending

The leading semicolon in immediately-invoked function EXPRESSIONS prevent errors when appending the file during concatenation. This concat is to a file containing an EXPRESSION improperly concluded with a semicolon.

Concatenate

The purpose to include semicolon is to safely concatenate several JS files into one.

Preceding Code

A leading semicolon protects from preceding code, which may have been improperly closed. A semicolon prevents this from occurring. If this is the case, then adding a semicolon will fix it.

Let’s see an example. Let’s say we are concatenating two files with self-invoking functions:

Function ONE

(function(){...ONE...})()

Function TWO 

(function(){...TWO...})()

Now concatenating it will give the following result:

(function(){...ONE...})() (function(){...TWO...})()

Function TWO can have a leading semicolon:

!(function(){...TWO...})()

Now after concatenation: 

(function(){...ONE...})();(function(){...TWO...})()


Discussion

No Comment Found