Closure function using outer variable instead of nearest variable [duplicate] - javascript

This question already has answers here:
How do JavaScript closures work?
(86 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed last month.
I don't understand why the functions are taking the outer scoped variable instead of the nearest one when called as callback.
function outerFn(){
let x = 1;
function log(){
console.log(x);
};
function run(fn){
let x = 100;
fn();
}
run(log);
};
outerFn();
I was expecting the run to log 100 instead of 1.

"x" is being closed over by your "log()" function. Your "log()" function does not have access to "x" declared in "run()".

Related

Is there a way to use a variable inside an arrow function but initialize it inside another function where the arrow function is also called? [duplicate]

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.

Why the function's log is the function itself? [duplicate]

This question already has an answer here:
Why is the named IIFE logged instead of the variable with the same name?
(1 answer)
Closed 2 years ago.
I have encountered a problem that if the IFEE function's name is the same with variable's name in it. the output is the function itself. Why?
var b = 10;
(function b() {
b = 20;
console.log(b);
})();
Named function expressions create a read only variable in their own scope which matches their name and references themselves.
This is useful for writing recursive functions.

How to change the value of a passed variable in a function in javascript? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
Inside a function, I'd like to change the value of a global variable that is specified by the function's argument.
A = 1;
B = 2;
C = 3;
function myFunction(variable) {
variable += 10;
}
myFunction(A);
myFunction(B);
myFunction(C);
I'm looking for these results:
console.log(A); // 11 expected
console.log(B); // 12 expected
console.log(C); // 13 expected
I can't output the new value via a return statement because it's computed inside the callback function of a post request.
How to achieve this?
It's not possible. This is called shadowing, that's that your argument is other variable than global, so A in myFunction is other that A in global
Maybe trying to run with apply() and using this.variable inside function would help

Will JavaScript Closure save all variables in its scope, or only those the closure referred? [duplicate]

This question already has answers here:
Over how much of its enclosing scope does a (javascript) closure close?
(2 answers)
Closed 5 years ago.
We knew that
function foo () {
var x = 10;
var y = 20;
function bar () {
return x + 1;
}
bar(); // 11
}
The function bar create a closure, and save the reference of x.
But what about the variable y? Will the closure bar created hold its reference? I tried it in Chrome Dev Tools, and it shows only x in the [[Scopes]] field, without y. But I can not find any articles about that.
Does it means the closure creation will only pick what it need to save?
Scopes are chained together and it'll check with parent scope when reference cannot be find. This question is answered before and you can reference it here:
Scope Chain in Javascript

How can I pass a value from one function to another function which has object and without declaring a global variable in JS? [duplicate]

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);

Categories