What is the correct term for variable shadowing in JavaScript?

The correct term is [Variable] Shadowing In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. This outer variable is said to be shadowed… Functions in JavaScript are just function-objects stored within variables … Read more

JavaScript plus sign in front of function expression

It forces the parser to treat the part following the + as an expression. This is usually used for functions that are invoked immediately, e.g.: +function() { console.log(“Foo!”); }(); Without the + there, if the parser is in a state where it’s expecting a statement (which can be an expression or several non-expression statements), the … Read more

What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?

It’s usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. The technical name for it is an Immediately Invoked Function Expression (IIFE). jQuery plugins are usually written like this. In Javascript, you can nest functions. So, the following is legal: function outerFunction() { … Read more

Why do you need to invoke an anonymous function on the same line?

Drop the semicolon after the function definition. (function (msg){alert(msg)}) (‘SO’); Above should work. DEMO Page: https://jsfiddle.net/e7ooeq6m/ I have discussed this kind of pattern in this post: jQuery and $ questions EDIT: If you look at ECMA script specification, there are 3 ways you can define a function. (Page 98, Section 13 Function Definition) 1. Using … Read more

Location of parenthesis for auto-executing anonymous JavaScript functions?

They’re virtually the same. The first wraps parentheses around a function to make it a valid expression and invokes it. The result of the expression is undefined. The second executes the function and the parentheses around the automatic invocation make it a valid expression. It also evaluates to undefined. I don’t think there’s a “right” … Read more

What is the purpose of a self executing function in javascript?

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: (function() { var … Read more

What is the (function() { } )() construct in JavaScript?

It’s an Immediately-Invoked Function Expression, or IIFE for short. It executes immediately after it’s created. It has nothing to do with any event-handler for any events (such as document.onload). Consider the part within the first pair of parentheses: (function(){})();….it is a regular function expression. Then look at the last pair (function(){})();, this is normally added … Read more