What is this practice called in JavaScript?
The pattern is called self-invocation, a self-invoking function. It can create a closure, but that is an effect of the pattern (perhaps the intended effect), not the pattern itself.
The pattern is called self-invocation, a self-invoking function. It can create a closure, but that is an effect of the pattern (perhaps the intended effect), not the pattern itself.
Your assumptions are almost correct. Let’s review those first. It assigns the return of a self-executing function This is called an Immediately-invoked function expression or IIFE It defines a local variable within this function This is the way of having private object fields in JavaScript as it does not provide the private keyword or functionality … Read more
From Douglass Crockford’s style convention guide: (search for “invoked immediately”) When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and not the function itself. So, basically, he feels it makes more … Read more
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
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
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
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
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
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
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