Correction suggested by Guffa: The function is executed right after it's created, not after it is parsed. The entire script block is parsed before any code in it is executed. Also, parsing code doesn't automatically mean that it's executed, if for example the IIFE is inside a function then it won't be executed until the function is called.
$ = function() { alert('I am in the $ function'); } JQuery is a very famous JavaScript library and they have decided to put their entire framework inside a function named jQuery. To make it easier for people to use the framework and reduce typing the whole word jQuery every single time they want to call the function, they have also created an alias for it. That alias is $. Therefore $ is the ...
508 It's all about variable scoping. Variables declared in the self executing function are, by default, only available to code within the self executing function. This allows code to be written without concern of how variables are named in other blocks of JavaScript code. For example, as mentioned in a comment by Alexander:
12 The function* type looks like it acts as a generator function for processes that can be iterated. C# has a feature like this using "yield return" see 1 and see 2 Essentially this returns each value one by one to whatever is iterating this function, which is why their use case shows it in a foreach style loop.
About __func__: "The identifier __func__ is implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration: static const char __func__[] = "function-name"; appeared, where function-name is the name of the lexically-enclosing function. This name is the unadorned name of the ...
(function(doc){ doc.location = '/'; })(document);//This is passed into the function above As for the other questions about the plugins: Type 1: This is not a actually a plugin, it's an object passed as a function, as plugins tend to be functions. Type 2: This is again not a plugin as it does not extend the $.fn object. It's just an extenstion of the jQuery core, although the outcome is the ...
function foo() {} Note that there’s no semicolon; this is just a function declaration. You would need an invocation, foo(), to actually run the function. Now, when we add the seemingly innocuous exclamation mark: !function foo() {} it turns it into an expression. It is now a function expression.
A function is outside the namespace of the rest of the program. It is like a separate program that can have the same variable names as used in the calling program, and whatever it does to them does not affect the state of those variables with the same name in the calling program.