Access local variable from the global context? [duplicate] - javascript

This question already has answers here:
What is a 'Closure'?
(22 answers)
How do JavaScript closures work?
(86 answers)
Closed 5 years ago.
function wrapValue(n) {
var localVariable = n;
return function() { return localVariable; };
}
var wrap1 = wrapValue(1);
var wrap2 = wrapValue(2);
console.log(wrap1());
// → 1
console.log(wrap2());
// → 2
In this example how is it allowed to access localVariable, which is local to wrapValue() function, from the global environment?
The book, included this example, stated the reason as follows:
"multiple instances of the variable can be alive at the same time".
But I didn't understand.

Your function sets a private value (localVariable) when called and returns it immediately so you can assign it to a another variable without change the private var inside function.

Related

why my global array has been updated after a function but my other global variable doesn't? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
JS function not updating an object
(5 answers)
Closed 1 year ago.
Imagine two situations below: We have an array which is a global variable and it will be updated after a function. On the other hand as you can see in commented code my other global variable doesn't updated after a function?
I learnt about pass by reference and value, but i am wondering how come array has been passes by reference to the function but "a" variable has been passes as value?
var a=[1,2,3,4,5];
function adder(arrey, item) {
arrey.push(item);
return arrey.shift();
}
console.log(a);
console.log(adder(a,6));
console.log(a);
var a =10
function adder (num) {
num+=1;
return num;
}
console.log(a);
adder(a);
console.log(a)

Reference of variable [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Pass variables by reference in JavaScript
(16 answers)
Closed 3 years ago.
I would like to have on a separate script some logic and I need to let it know on which global variable all of my operations must be done: how can I pass to a function a reference to the variable?
function setVariable(variable){
//Here I let the script know on which variable it should work
//Example var x = reference to 'variable'
}
function run(){
if(x === 0){ ...
... }
}
I cannot "copy" the variable value because it's global and it can change anytime and this script has to read the updated value.

How to get the scope from where a function was called? [duplicate]

This question already has answers here:
How to access values from <function scope>'s Closure in Chrome Developer tool's Watch panel?
(2 answers)
What way (if any) can I get the reference to anonymous function's closure in javascript?
(3 answers)
Is it possible to gain access to the closure of a function?
(4 answers)
Closed 5 years ago.
I'm trying to create a javascript expression parser, able to parse an expression and get the declared variables in its body, without executing the function:
function test(expressionFn) {
var expression = getExpression(expressionFn);
// get value expression.expressionRight in scope
// not work -> var valueRigthInScope = expressionFn.prototype.constructor[[[Scopes]]][expression.expressionRight];
console.log(expressionFn.prototype); // see ".constructor[[[Scopes]]]" in debug tools [F12]
}
function getExpression(expressionFn) {
var strAfterReturn = expressionFn.toString().split("return")[1].trim();
let strExpression = strAfterReturn.split(";")[0].split(" ");
return {
expressionLeft: strExpression[0],
operator: strExpression[1],
expressionRight: strExpression[2]
};
}
function start(){
var myValue = "This is value!";
test(function(x) { return x.prop1 == myValue; });
}
start();
<h1>See console</h1>
Example in JsFiddle.
But I can not access the scope of the function to get the value of the variable myValue, for example.
In the Google Chrome console I managed through the function to get the scope where the variable myValue is declared, but I can not access that same scope in javascript.
Follows the image of the Google Chrome console:
How can I access [[Scopes]] in the image?

Why can I delete a variable declared a = 1 and not one declared var b = 1 in javascript [duplicate]

This question already has answers here:
How can I unset a JavaScript variable?
(12 answers)
Closed 7 years ago.
Let's suppose we are in the global scope:
When I declare a variable in JS:
a = 1
which I know is not the proper way (but it is not the question).
I can use:
delete a (> returns true)
But when I declare:
var b = 1
I can't use:
delete b (> returns false)
Can anyone explain that behaviour?
delete is only effective on an object's properties. It has no effect on variable or function names.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
a becomes/is ultimately interpreted as window.a, while b clearly refers to a local variable.

Javascript: this.varName = "" vs var varName = "" [duplicate]

This question already has answers here:
Difference between this and var in a function
(5 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 9 years ago.
I'm new to JS so noob question:
I see functions which define vars inside them:
function functionName(){
this.something = "";
};
If I understand correctly, something is a local variable? Why is it being defined as this.something = '' as opposed to var something = ''?
Is there any difference, and if so what is it?
It sets the attribute something of this. What this refers to depends on how functionName is invoked, but typically it's an object created with new:
var foo = new functionName();
console.log(foo.something);
If you'd use var something inside the function, the variable would be inaccessible from outside, so you couldn't do foo.something in the above example.
var someThing
is a local variable - meaning it exists within the scope of the current block
this.someThing
is an instance variable - meaning it belongs to the object and is visible in all methods of that object.

Categories

Resources