self-executing-function

How to using ES6 Arrow function to realize Immediately-Invoked Function Expression (IIFE))? [closed]

主宰稳场 提交于 2019-12-30 07:50:47
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . How to using ES6 Arrow function to realize IIFEImmediately-Invoked Function Expression? Here is my demo codes, and it had tested passed! // ES 6 + IIFE (() => { let b = false; console.log(`b === ${b}!`); const print = `print()`; if(window.print){ b = true; console.log(`b === ${b}!`); } let x = () =

Self-executing Java methods

◇◆丶佛笑我妖孽 提交于 2019-12-22 00:55:10
问题 In JavaScript, it is possible to write a self-executing function like this: (function foo() { console.log("bar"); }()); I'm looking to do this in Java. So for example: // This code does not work obviously public static void main(String[] args) { (foo() { System.out.println("bar"); }()); } Is there such a thing? 回答1: That javascript isn't really creating a "self-executing" function. It's defining a function, and then immediately executing it. Java doesn't let you define standalone functions,

Javascript self executing functions and variable scope

别来无恙 提交于 2019-12-13 05:12:32
问题 Could someone explain to me this behavior? var obj = function() { var _bar = 10; function i_bar(){return ++_bar;} return { bar : _bar, i_bar: i_bar } }(); obj.bar // prints 10, OK obj.i_bar() // prints 11, OK obj.bar = 0 // prints 0, OK obj.i_bar() // prints 12, NOK Since the only variable is _bar , shouldn't the last obj.i_bar() have printed 1 instead of 12 ? 回答1: Your bar is not the same references as what i_bar is referencing. Value types are not by reference, so you are copying bar into

Accessing Shadowed Variable in Self Executing Function

拥有回忆 提交于 2019-12-13 01:09:39
问题 In the following example, is there any way to get a reference to the someValue variable declared outside someFunction from within someFunction or is it completely obscured by the function's parameter of the same name. I appreciate that I could attach it to window and access it from within the function using this , but is there a way of accessing it in this situation? [Edit] To clarify. I understand that the parameter is shadowing the variable. Obviously changing the name of the parameter

Javascript - self-executing functions : why to use them if I can create local scope with not self-executing functions?

会有一股神秘感。 提交于 2019-12-08 12:46:23
问题 I know there are a lot of posts here and elsewhere about selfexecuting functions but I still have some questions after reading posts. why would I ever assign a self-executing function to a variable? If seems that they execute themselves anyways. var myFunc=(function() { console.log('Hello World'); })(); I read a lot that the reason to use self-executing functions is to keep variables private. If I have a not self-executing function, everything I define inside that function is gonna be private

Modify arguments in self executing function

吃可爱长大的小学妹 提交于 2019-12-06 05:41:15
I want to be able to modify the arguments passed to a self executing function. Here is some sample code: var test = 'start'; (function (t) {t = 'end'} )(test); alert(test) //alerts 'test' And here is a fiddle . The variable test has not changed. How can I alter it, as in pass-by-reference? Pass in an object , it is pass-by-reference : var test = { message: 'start' }; (function (t) {t.message = 'end'} )(test); alert(test.message) FYI, Array is also pass-by-reference . You cannot do that (well, precisely that) in JavaScript. You can do something like this, however: var testBox = { test: "hello"

How to using ES6 Arrow function to realize Immediately-Invoked Function Expression (IIFE))? [closed]

倖福魔咒の 提交于 2019-12-01 02:01:34
How to using ES6 Arrow function to realize IIFE Immediately-Invoked Function Expression ? Here is my demo codes, and it had tested passed! // ES 6 + IIFE (() => { let b = false; console.log(`b === ${b}!`); const print = `print()`; if(window.print){ b = true; console.log(`b === ${b}!`); } let x = () => { if(b){ console.log(`Your browser support ${print} method.`); }else{ alert(`Your browser does not support ${print} method.`); console.log(`Your browser does not support ${print} method.`); }; } x(); })(); const dcs = `IIFE: Douglas Crockford's style`; // ES 5 + IIFE is OK (function(){ alert(

what is self-executing anonymous function or what is this code doing?

余生长醉 提交于 2019-11-29 23:22:44
var module = {}; (function(exports){ exports.notGlobalFunction = function() { console.log('I am not global'); }; }(module)); function notGlobalFunction() { console.log('I am global'); } notGlobalFunction(); //outputs "I am global" module.notGlobalFunction(); //outputs "I am not global" Can anyone help me understand what's going on here? I get that if you call notGlobalFunction() , it will just call the second function. But what is var module = {} doing? and why is it called again inside the first function? It says this is commonly known as a self-executing anonymous function but I don't really

what is self-executing anonymous function or what is this code doing?

放肆的年华 提交于 2019-11-28 20:41:17
问题 var module = {}; (function(exports){ exports.notGlobalFunction = function() { console.log('I am not global'); }; }(module)); function notGlobalFunction() { console.log('I am global'); } notGlobalFunction(); //outputs "I am global" module.notGlobalFunction(); //outputs "I am not global" Can anyone help me understand what's going on here? I get that if you call notGlobalFunction() , it will just call the second function. But what is var module = {} doing? and why is it called again inside the

Self Executing Anonymous Functions via Lambdas

一个人想着一个人 提交于 2019-11-28 00:39:16
问题 In javascript, there's the common pattern of creating an anonymous function and immediately invoking it (usually this is called a self-executing anonymous function or an immediately-invoked function expression). With Java 8 lambdas, is there a standard way to replicate this behaviour? Something like (() -> doSomething())() . This question asks basically the same question, but for Java 7. I'm explicitly looking for constructs which utilize lambdas. 回答1: Not without declaring the type as well.