Semicolon Before Self-Invoking Function

Semicolon before self-invoking function?

If you concatenate two files with self-invoking functions together that look like this:

File A:

(function(){...A...})()

File B:

(function(){...B...})()

File A+B:

(function(){...A...})()(function(){...B...})()

You have two statements without separator. This happens when you cat files together and then minify them.

Now the author of file B puts a semicolon in front:

File B2:

;(function(){...B2...})()

And you'll get a working script:

(function(){...A...})();(function(){...B2...})()

What is the purpose of a semicolon before an IIFE?

It's there to prevent any previous code from executing your code as the arguments to a function.

i.e.

mybrokenfunction = function(){

} //no semicolon here
(function(g){

})(this);

will execute mybrokenfunction with your anonymous function as its argument:

mybrokenfunction = function(){}(function(g){})(this);

If you could guarantee that there won't be an unterminated (no semicolon) function before yours, you could omit the starting semicolon, but you can't, so it's just safer to put that extra semicolon in.

Why is there sometimes a semicolon before an anonymous function?

By putting it there, it ensures the preceding statement was closed. It's particularly important when you are minifying JavaScript code. One of the most common problems there is when you don't have one file that ends with neither a new line nor a semicolon and gets merged with one that starts with neither. That effectively merges the last statement of the first file with the first line of the second file resulting in syntax errors.

Anonymous self-invoking function is automatically passed as an argument to above declared local function. Why?

Remove some newlines and you can see why:

let myfunc = function(a) {
return function localScope(b) {
console.log(a + b + " this")
}
}(function () {
console.log('test')
})()

And function(){}() is actually valid syntax to call that anonymous function. You can use a defensive semicolon to properly split the statements:

let myfunc = function(a) {
return function localScope(b) {
console.log(a + b + " this")
}
};

(function () {
console.log('test')
})()

What does the first semicolon imply? ;( function( ) { } ) ( );

It is a defensive semicolon, this is in case someone concatenates some JavaScript before your code, and this concatenated code forgot to put a terminating semicolon.

Error with multiple self-invoking functions in one script: a case for a semicolon

You forgot the semicolons:

(function () {
console.log('1/2')
})();

(function () {
console.log('2/2')
})();

Otherwise the returned value of the previous expression (undefined) tries to execute the next expression. Obviously undefined is not a function.

In the semicolonless JavaScript world, you'll often see the semicolon preceding any raw expression, such as ():

;(function(){}())
;['1','2','3'].map(Number)

JavaScript will attempt to fill in the semicolons you "forgot" but those expressions are ambiguous so you need to add it.

What does the leading semicolon in JavaScript libraries do?

It allows you to safely concatenate several JavaScript files into one, to serve it quicker as one HTTP request.

what does the semikolon mean at the begining of this function?

It is do the same function semicolon at the end.
I think it writed this way to prevent errors when script will be minimized.

what is the purpose to write function like that?

To catch events

what does "extend" mean here ?

Merge to object in one. Take a look at jquery docs.



Related Topics



Leave a reply



Submit