iife

Why are parentheses required around JavaScript IIFE? [duplicate]

跟風遠走 提交于 2019-11-26 14:46:05
问题 This question already has an answer here: Explain the encapsulated anonymous function syntax 10 answers I'm reading up on JavaScript IIFE and so far the understand concept, but I am wondering about the outside parenthesis. Specifically, why are they required? For example, (function() {var msg='I love JavaScript'; console.log(msg);}()); works great, but function() {var msg='I love JavaScript'; console.log(msg);}(); generates a syntax error. Why? There are lots of discussions on IIFE, but I'm

understanding $ vs. jQuery in iife instead of $

跟風遠走 提交于 2019-11-26 11:38:03
问题 I am trying to understand if there is any difference between: (function($){ ... })(jQuery); vs. (function($){ ... })($); Note the jQuery was replaced with a $. Is this ok? Is it not used anywhere because it can\'t work? It works but maybe it is non-standard? Can someone please explain this if it is an error or if it is ok? Thanks 回答1: Other JavaScript frameworks may also use $ as a shortcut. To guarantee that $ is jQuery inside your function, pass jQuery and not $ at the end. This type of

What is this JavaScript pattern called and why is it used?

☆樱花仙子☆ 提交于 2019-11-26 11:08:55
I'm studying THREE.js and noticed a pattern where functions are defined like so: var foo = ( function () { var bar = new Bar(); return function ( ) { //actual logic using bar from above. //return result; }; }()); (Example see raycast method here ). The normal variation of such a method would look like this: var foo = function () { var bar = new Bar(); //actual logic. //return result; }; Comparing the first version to the normal variation, the first seems to differ in that: It assigns the result of a self-executing function. It defines a local variable within this function. It returns the

Dollar sign before self declaring anonymous function in JavaScript?

こ雲淡風輕ζ 提交于 2019-11-26 09:28:54
问题 What is the difference between these two: $(function () { // do stuff }); AND (function () { // do stuff })(); 回答1: The first uses jQuery to bind a function to the document.ready event. The second declares and immediately executes a function. 回答2: $(function() {}); is a jQuery shortcut for $(document).ready(function() { /* Handler for .ready() called. */ }); While (function() {})(); is a instantly invoked function expression, or IIFE. This means that its an expression (not a statement) and it

Advanced JavaScript: Why is this function wrapped in parentheses? [duplicate]

给你一囗甜甜゛ 提交于 2019-11-26 04:32:03
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: What is the (function() { } )() construct in JavaScript? I came across this bit of JavaScript code, but I have no idea what to make out of it. Why do I get \"1\" when I run this code? What is this strange little appendix of (1) and why is the function wrapped in parentheses? (function(x){ delete x; return x; })(1); 回答1: There are a few things going on here. First is the immediately invoked function expression

!function(){ }() vs (function(){ })()

Deadly 提交于 2019-11-26 04:25:41
问题 While reviewing some of the code written in the Twitter Bootstrap Javascript, it looks like they\'re calling immediately invoked anonymous functions like this: !function( $ ) { ... }(window.jQuery || window.ender); Where I\'ve traditionally seen this same thing accomplished this way: (function($) { ... })(window.jQuery || window.ender); The first way seems a bit hacky, and I\'m not sure if there is any benefit or reason for doing it this way rather than the second way? Note that I understand

Variable shadowing in JavaScript

自古美人都是妖i 提交于 2019-11-26 02:06:41
Below we have an IIFE which (like any function) creates a local scope. Inside that scope there is a parseInt function. Now, since there already is a global function in the browser with that name, the local function will overshadow the global parseInt function - inside the IIFE any call to parseInt will call the local function, and not the global one. (The global function can still be referenced with window.parseInt .) parseInt('123', 10); // the browser function is called (function() { function parseInt() { return 'overshadowed'; } parseInt('123', 10); // the local function is called })();

Immediate function invocation syntax

徘徊边缘 提交于 2019-11-26 01:44:57
问题 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

Variable shadowing in JavaScript

故事扮演 提交于 2019-11-26 01:00:33
问题 Below we have an IIFE which (like any function) creates a local scope. Inside that scope there is a parseInt function. Now, since there already is a global function in the browser with that name, the local function will overshadow the global parseInt function - inside the IIFE any call to parseInt will call the local function, and not the global one. (The global function can still be referenced with window.parseInt .) parseInt(\'123\', 10); // the browser function is called (function() {

JavaScript plus sign in front of function name

社会主义新天地 提交于 2019-11-25 23:59:06
问题 I\'ve been looking on info about self-invoking functions, and somewhere I stumbled on this notation: +function(){} Can someone explain to me what the + sign in front of the function means/does? 回答1: 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