iife

declaring a variable twice in IIFE

允我心安 提交于 2019-12-02 22:23:45
I came through this fun quiz on the internet. console.log((function(x, f = (() => x)){ var x; var y = x; x = 2; return [x, y, f()] })(1)) and the choices were: [2,1,1] [2, undefined, 1] [2, 1, 2] [2, undefined, 2] I picked solution 2 TBH, basing that on that x has been redefined, y was declared and defined with no value, and that f has a different scope hence getting the global x memory spot than function x memory spot. However, I tried it in jsbin.com and I found it was solution 1, while I was not sure why that happened I messed with the function body and I removed var x from the function

Immediately invoked function expression without using grouping operator

自作多情 提交于 2019-12-02 21:19:02
I'm trying to immediately invoke a function without using IIFE pattern (enclosing a function definition inside parentheses). Here I see two scenarios: When a function declaration is invoked immediately: gives SyntaxError . When a function expression is invoked immediately: executes successfully . Example 1: gives SyntaxError //gives `SyntaxError` function() { console.log('Inside the function'); }(); Example 2: Executes without any error // Executes without any error var x = function() {console.log('Inside the function')}(); // Inside the function So, I have these doubts: With this approach,

Javascript Syntax: Immediately Invoked Function Expression (IIFE) with parameters

那年仲夏 提交于 2019-12-02 06:48:24
问题 I have always seen code like this: (function(){ //code here })(); How does this code work? Which function receives which parameters? (function(factory){ //code here }(function($){ //other code here })); 回答1: function($){ //other code here } This block is passed as a parameter to the outer IIFE. It might be clearer to write it like this: var factoryDef = function($) { ... }; (function(factory) { // this is the outer IIFE var someVar = {}; // you can call: factory(someVar); // the previous line

Javascript Syntax: Immediately Invoked Function Expression (IIFE) with parameters

有些话、适合烂在心里 提交于 2019-12-02 02:31:21
I have always seen code like this: (function(){ //code here })(); How does this code work? Which function receives which parameters? (function(factory){ //code here }(function($){ //other code here })); function($){ //other code here } This block is passed as a parameter to the outer IIFE. It might be clearer to write it like this: var factoryDef = function($) { ... }; (function(factory) { // this is the outer IIFE var someVar = {}; // you can call: factory(someVar); // the previous line calls factoryDef with param someVar }(factoryDef)); So factory(someVar) is the same as factoryDef({}) ; the

Difference in these tiny syntax variations for an IIFE? [duplicate]

自作多情 提交于 2019-12-01 14:50:05
This question already has an answer here: Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript? [duplicate] 3 answers Sometimes I see this: (function() { alert("hi"); })(); And sometimes I see this: (function() { alert("hi"); }()); Note the placement of the closing paren for the function object. What is the difference? I can't figure it out. Is either preferable for any reason? Edit: Also, this does not work: function() { alert("hi"); }(); Which seems weird, since it is valid if wrapped in parentheses, as in example 2. I don't understand why wrapping it

Difference in these tiny syntax variations for an IIFE? [duplicate]

我的梦境 提交于 2019-12-01 13:28:54
问题 This question already has answers here : Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript? [duplicate] (3 answers) Closed 5 years ago . Sometimes I see this: (function() { alert("hi"); })(); And sometimes I see this: (function() { alert("hi"); }()); Note the placement of the closing paren for the function object. What is the difference? I can't figure it out. Is either preferable for any reason? Edit: Also, this does not work: function() { alert("hi

What is the “x = x || {}” technique in JavaScript - and how does it affect this IIFE? [duplicate]

好久不见. 提交于 2019-12-01 09:53:31
This question already has an answer here: What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript? 7 answers First, a pseudo code example: ;(function(foo){ foo.init = function(baz) { ... } foo.other = function() { ... } return foo; }(window.FOO = window.FOO || {})); Called like so: FOO.init(); My question: What is the technical name/description of: window.FOO = window.FOO || {} ? I understand what the code does... See below for my reason(s) for asking. Reason for asking: I'm calling the passed in global like so: ;(function(foo){ ... foo vs.

Different ways to execute IIFE?

∥☆過路亽.° 提交于 2019-12-01 09:35:53
Is there any difference between (function (){alert('')} ()) vs (function (){alert('')}) () Both works but when should I use each ? The wrapping parentheses are only there to force the parser to parse the construct as a function expression, rather than a function declaration. This is necessary because it's illegal to invoke a function declaration, but legal to invoke a function expression. To that end, it doesn't matter where the invoking parentheses go. It also doesn't matter how you force the function to be parsed as an expression. The following would work just as well: !function () { alert('

What is the “x = x || {}” technique in JavaScript - and how does it affect this IIFE? [duplicate]

旧时模样 提交于 2019-12-01 06:41:58
问题 This question already has answers here : What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript? (7 answers) Closed 4 years ago . First, a pseudo code example: ;(function(foo){ foo.init = function(baz) { ... } foo.other = function() { ... } return foo; }(window.FOO = window.FOO || {})); Called like so: FOO.init(); My question: What is the technical name/description of: window.FOO = window.FOO || {} ? I understand what the code does... See

Enums in TypeScript: what is the JavaScript code doing?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 03:15:27
The following TypeScript: enum PrimaryColors { Red, Green, Blue }; Produces the following JavaScript: var PrimaryColors; (function (PrimaryColors) { PrimaryColors[PrimaryColors["Red"] = 0] = "Red"; PrimaryColors[PrimaryColors["Green"] = 1] = "Green"; PrimaryColors[PrimaryColors["Blue"] = 2] = "Blue"; })(PrimaryColors || (PrimaryColors = {})); ; I am embarrassed to admit that I don't understand what the JavaScript is doing. The function in parentheses is assigning string values using another assignment as the index/key. I have not seen anything like this before. And what is the purpose of the