This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Advanced JavaScript: Why is this function wrapped in parentheses? [duplicate]
(4 answers)
Closed 4 years ago.
Can anybody explain below code? All I know is it is anonymous function but what is (0) doing?
var output = (function(x) {
delete x;
return x;
})(0);
console.log(output);
Why output of above code comes zero. Can anybody explain?
That's because what you're doing is creating a function and then calling it immediately where x = 0. Your function is returning x, and thus 0.
As for what anonymous functions are, they are basically a function that gets stored in a variable. You call it from the variable instead of by name. So, for example:
var output = function (x) { return x;};
Can be called like this:
output(0);
As opposed to the normal way like this:
function myOutput(x) {
return x;
}
myOutput(0);
Related
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
What is lexical scope?
(21 answers)
Closed 10 months ago.
I'm trying to access the variable c that is initialized inside myFunc when I call sum.
let sum = (a,b) => {
let result = a + b + c;
return result;
}
function myFunc(){
let c = 5;
return sum(1,2);
}
console.log(myFunc());
But I'm getting an error because of the c that I use inside the arrow function. Don't know if I'm explaining right but I think you get the point by seeing the code above.
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 3 years ago.
I was studying on JS Decorators and don't understand how the wrapper is able to access the inner functions argument. The snippet works, but I don't understand why the anonymous function 'function(val)' gets access to val, the argument of slow().
// https://codepen.io/lideebe/pen/VOjGvb
// simple function that gets wrapped
function slow(x){
return x * 3;
}
// the wrapper function
function cacheDecorator(func){
return function(val){ // How does this anonymous function get access to val?
return func(val)
}
}
// do the wrap
slow = cacheDecorator(slow);
// call the function
console.log(slow(2));
Output is 6 and that is correct.
It's because cacheDecorator returns a function which takes a parameter val, and when you call that returned function with the value 2, it just gets accessed like normal. So slow, after you reassign it, is:
slow = function(val) {
return val * 3;
}
So val is used as the parameter for the argument passed when the newly wrapped function is called.
This question already has answers here:
ES6 immediately invoked arrow function
(4 answers)
Closed 4 years ago.
Sometimes I want to assign a variable the result of an Arrow function, but if I assign the Arrow Function to that variable it get the function and not the return of that Function as is obvious.
let theNumber2 = ()=>{return 2};
theNumber2 will contain the function and only if I do theNumber2() I will get returned 2.
Is there a way to do something like this to get directly the return of the arrow function? For example:
let theNumber2 = ()=>{return 2}(); // Note the () at the final
Does there exist something to do that? I mean, to assign the arrow function called?
Just wrap in parenthesis and call the function.
let theNumber2 = (() => { return 2; })();
console.log(theNumber2);
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 9 years ago.
I'm trying to understand the difference between :
var x = function () { ....}
(function () { ....} ) ();
I get it that the first function will put the results on x.
that and when exactly the second one will be fired? and why do i need the (); at the end?
This is an example of the Immediately-invoked function expression.
The function is executed immediately because () is how JavaScript calls functions. The syntax might be confuse you, because the function does not have a name, but ( function(){} )() just immediately calls the function with no arguments.
This question already has answers here:
Passing one variable from one function to another function in java script phone gap [closed]
(2 answers)
How can I pass a value from one function to another function without declaring a global variable in JS?? [duplicate]
(7 answers)
Closed 9 years ago.
I just want to pass the value from my new1 and new new2 function to new3 function, where new1 and new2 containing object..
function new1(obj1){
var a = obj1.x;
}
function new2(obj2){
var c=obj2.y;
}
function new3(){
if(a<c){
dosomething();
}
}
You already have access to the properties in question since they must be passed to the first two functions. There is no need to operate on the private a and c variables.
function new1(obj1){
var a = obj1.x;
}
function new2(obj2){
var c=obj2.y;
}
function new3(obj1,obj2){
if(obj1.x < obj2.y){
dosomething();
}
}
new1(obj1);
new2(obj2);
new3(obj1,obj2);