iife

Immediate functions JavaScript

自古美人都是妖i 提交于 2021-02-17 19:13:31
问题 Stoyan Stefanov says in JavasScript Patterns that: "you need an immediated function to wrap all your code in its local scope and not to leak any variables to the global scope" page 70. Here is his example (function() { var days = ['Sun','Mon']; // ... // ... alert(msg); }()); But surely because days is defined as a var, it will just be functional scope? The only benefit of the immediate function is the function is invoked immediately. There is no scope advantage. Corrcet? 回答1: It's not about

Hoisting order with IIFE involved, specific example

馋奶兔 提交于 2021-01-29 06:31:49
问题 I came across this code: var myVar = 'foo'; (function() { console.log('Original value was: ' + myVar); var myVar = 'bar'; console.log('New value is: ' + myVar); })(); Questions: Is IIFE hoisted to the top before global myVar ? 1a. IF it is, is it executed before global myVar is declared? In IIFE I get undefined first, and bar second. What is the behind the scenes order of execution in IIFE? 回答1: The IIFE is an expression, not a statement, so no it is not hoisted. var myVar inside the IIFE is

Hoisting order with IIFE involved, specific example

北城以北 提交于 2021-01-29 06:21:26
问题 I came across this code: var myVar = 'foo'; (function() { console.log('Original value was: ' + myVar); var myVar = 'bar'; console.log('New value is: ' + myVar); })(); Questions: Is IIFE hoisted to the top before global myVar ? 1a. IF it is, is it executed before global myVar is declared? In IIFE I get undefined first, and bar second. What is the behind the scenes order of execution in IIFE? 回答1: The IIFE is an expression, not a statement, so no it is not hoisted. var myVar inside the IIFE is

Passing argument to Go IIFE (following javascript example)

不想你离开。 提交于 2020-08-11 22:23:40
问题 I'm used to programming in javascript where I can do the following to pass an argument into an immediately-invoked function expression: (function(twoSeconds) { // do something with "twoSeconds" here })(2 * 1000); So I expected to be able to do something similar in Go, as below. However, it doesn't seem to work. func (twoSeconds) { // build error: "twoSeconds" undefined }(time.Second * 2) So I have to do this instead: func () { twoSeconds := time.Second * 2 }() Therefore, my question is how

Passing argument to Go IIFE (following javascript example)

試著忘記壹切 提交于 2020-08-11 22:22:41
问题 I'm used to programming in javascript where I can do the following to pass an argument into an immediately-invoked function expression: (function(twoSeconds) { // do something with "twoSeconds" here })(2 * 1000); So I expected to be able to do something similar in Go, as below. However, it doesn't seem to work. func (twoSeconds) { // build error: "twoSeconds" undefined }(time.Second * 2) So I have to do this instead: func () { twoSeconds := time.Second * 2 }() Therefore, my question is how

When IIFE return a value, where does the value exist?

天涯浪子 提交于 2020-01-24 20:57:35
问题 When I tried the example on babel Symbol in the example there was no return, so I added it cause I thought it should be(I am not sure if I am right). It logged in my console MyClass is not defined. If IIFE returns a Class, why it said MyClass is not defined? (function() { var key = Symbol("key"); function MyClass(privateData) { this[key] = privateData; } MyClass.prototype = { doStuff: function() { } }; return MyClass //this is no return for the original example })(); var c = new MyClass(

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

天涯浪子 提交于 2020-01-16 04:49:06
问题 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 I

Testing javascript inside IIFE

纵然是瞬间 提交于 2020-01-13 07:24:50
问题 My team leader wants me to wrap my js code inside an IIFE immediatly-invoked function expression. I can't figure out how to spyOn in my jasmine spec file. How would I spyOn the follow: (function(){ function_1(){...} function_2(){...} }); spyOn(window,'function_1') doesn't work. Passing window into the IIFE doesn't work. Trying both (x=function()... and (function x() ... then spyOn(x,'function_1') doesn't work either. I couldn't find anything on-line about this. 回答1: @user29998, if you can

In JavaScript, what is the advantage of !function(){}() over (function () {})()? [duplicate]

时间秒杀一切 提交于 2020-01-09 08:26:11
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What does the exclamation mark do before the function? I've long used the following for self-executing, anonymous functions in JavaScript: (function () { /* magic happens */ })() Lately, I've started seeing more instances of the following pattern (e.g., in Bootstrap): !function () { /* presumably the same magic happens */ }() Anyone know what the advantage is of the second pattern? Or, is it just a stylistic