This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 4 years ago.
Why this code returns 43 as the result, I expect it to result 42. The code is as follows:
function say667() {
// Local variable that ends up within closure
var num = 42;
var say = function() { console.log(num); }
num++;
return say;
}
var sayNumber = say667();
sayNumber();
You've closed over the variable num, not the value the variable has at the time you define the function.
This is the order of events:
You assign 42 to num
You increment num to 43
You return a function and store it in sayNumber
You call that function, which reads the value of num, which is 43
Related
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Explanation of `let` and block scoping with for loops
(5 answers)
Closed 7 months ago.
I expect the calling of the first element in the array to print out the number 0. However, it prints 5. Does the function call have access to i of its parent function because I declare i as var? If I declare i as a let variable, it prints 0 as expected.
0-closureBug.js
function makeFunctionArray() {
const arr = []
for (var i = 0; i < 5; i++) {
arr.push(function() { console.log(i) })
}
console.log(i)
return arr
}
const functionArr = makeFunctionArray()
functionArr[0]()
This question already has answers here:
What is the temporal dead zone?
(3 answers)
Are variables declared with let or const hoisted?
(7 answers)
Closed 1 year ago.
Why is it logging "Reference Error" in the first case, why didn't it pick it from the global scope like in the second case?
let a = 10;
function test1() {
console.log(a);
let a = 100;
}
test1(); // Reference Error
let a = 10;
function test2() {
console.log(a);
}
test2(); //10
This question already has answers here:
How do JavaScript closures work?
(86 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
I have snippet below, where values become as array of functions. Each function contains a console.log(i); Where i was a local variable of the loop. However when i execute (values[1]()) the values array the console.log is printing the i values. I'm trying to understand how this is possible
var values = [];
for ( let i = 0; i < 2; i++ )
{
values.push(function() {
console.log(i);
})
}
console.log(values); /* output : Array [function() {
console.log(i);
}, function() {
console.log(i);
}]*/
values[0](); // output: 0
values[1](); // output: 1
This question already has answers here:
Determine original name of variable after its passed to a function
(9 answers)
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 7 years ago.
I know this is completely useless, but I'm nonetheless curious. Is it possible to do something like the below?
function getVariableName ( v )
{
// .... do something .....
// return the string "v"
}
var x = 69;
var y = "somestring";
console.log(getVariableName(x)); // prints "x" to the console
console.log(getVariableName(y)); // prints "y" to the console
function getArgNames(fn) {
var matches = fn.toString().match(/\(([a-z_, ]+)\)/i);
if (matches.length > 1) {
return matches[1].replace(/\s+/g, '').split(',');
}
return [];
}
That will return an array of the names of all arguments passed in to the function.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript Pass Variables Through Reference
How can you do something like this in javascript?
In PHP you can put a & in a function in front of the parameter to write directly back to the variable.. How can you do something like this in javascript?
In JavaScript, values are passed to functions by value. Objects, however, are passed by reference.
Passing values:
function myfunction(x)
{
// x is equal to 4
x = 5;
// x is now equal to 5
}
var x = 4;
alert(x); // x is equal to 4
myfunction(x);
alert(x); // x is still equal to 4
Passing objects:
function myobject()
{
this.value = 5;
}
var o = new myobject();
alert(o.value); // o.value = 5
function objectchanger(fnc)
{
fnc.value = 6;
}
objectchanger(o);
alert(o.value); // o.value is now equal to 6
Source: http://snook.ca/archives/javascript/javascript_pass
If you pass an object into a function it is always treated as passing a reference to it.
To have a similar effect for primitive values, you might wrap them in an object before like
var intVal = { val: 0 };
That way you can pass it to a function and still modify the actual variable.