iife

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

╄→尐↘猪︶ㄣ 提交于 2019-11-25 22:27:25
问题 I have been reading a lot of Javascript lately and I have been noticing that the whole file is wrapped like the following in the .js files to be imported. (function() { ... code ... })(); What is the reason for doing this rather than a simple set of constructor functions? 回答1: It's usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. jQuery plugins are usually written like this. In Javascript, you can nest

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

僤鯓⒐⒋嵵緔 提交于 2019-11-25 22:25:05
问题 I was reading some posts about closures and saw this everywhere, but there is no clear explanation how it works - everytime I was just told to use it...: // Create a new anonymous function, to use as a wrapper (function(){ // The variable that would, normally, be global var msg = \"Thanks for visiting!\"; // Binding a new function to a global object window.onunload = function(){ // Which uses the \'hidden\' variable alert( msg ); }; // Close off the anonymous function and execute it })(); Ok

Location of parenthesis for auto-executing anonymous JavaScript functions?

妖精的绣舞 提交于 2019-11-25 21:44:29
问题 I was recently comparing the current version of json2.js with the version I had in my project and noticed a difference in how the function expression was created and self executed. The code used to wrap an anonymous function in parenthesis and then execute it, (function () { // code here })(); but now it wraps the auto-executed function in parenthesis. (function () { // code here }()); There is a comment by CMS in the accepted answer of Explain JavaScript’s encapsulated anonymous function

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

我只是一个虾纸丫 提交于 2019-11-25 21:39:01
问题 In javascript, when would you want to use this: (function(){ //Bunch of code... })(); over this: //Bunch of code... 回答1: Its 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: (function(){ var foo = 3; alert(foo); })(); alert(foo); This will first alert "3" and then

Immediate function invocation syntax

我与影子孤独终老i 提交于 2019-11-25 18:51:11
There is a JSLint option, one of The Good Parts in fact, that "[requires] parens around immediate invocations," meaning that the construction (function () { // ... })(); would instead need to be written as (function () { // ... }()); My question is this -- can anyone explain why this second form might be considered better? Is it more resilient? Less error-prone? What advantage does it have over the first form? Since asking this question, I have come to understand the importance of having a clear visual distinction between function values and the values of functions. Consider the case where the