Reference of variable [duplicate] - javascript

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.

Related

Using outside variables inside an arrow function in Node.js [duplicate]

This question already has answers here:
How can I pass variable into an evaluate function?
(7 answers)
How do i return a value from page.evaluate() in puppeteer?
(3 answers)
Closed 1 year ago.
I'm kind of new to JavaScript but I'm using a function that takes another arrow function as a parameter. All I need to do in the function is simply set a value to some variable I have declared before it. From my understanding, var means it has global scope, so I'm not really sure why I cant use the variable. If I try to pass it in the parameter I get undefined. I'll paste the 2 lines of code giving me issues
var thing = events[i]["Address"];
await page.evaluate( () => document.querySelector('[class="form-control tt-input"]').value = thing)

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)

Pass window variable as parameter in Javascript [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 2 years ago.
Passing a window global variable through JS does not seem to be working. The following code is printing true:
window.nada = true;
tata(window.nada);
console.log(window.nada);
function tata(lala) {
lala = false;
}
How can I affect the window.nada global variable inside the tata function?
Technically, JavaScript uses call-by-sharing.
In practice, you'll have to pass the entire window object, as well as the name of the property you want to change:
tata(window, 'nada');
function tata(window, prop)
{
window[prop] = false;
}
Your window.nada is a primitive data type (Boolean).
Primitive data types are passed to the function per value and not per reference. So inside your tata function the lala variable does not know anything about the window.nada value

Get the name of the argument from inside the function? [duplicate]

This question already has answers here:
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Determine original name of variable after its passed to a function
(9 answers)
Closed 3 years ago.
Is it possible to get the name of the variable passed in as an argument from inside of the function, if a variable has been passed in?
function foo(bar){
// how to get the string `obj`?
}
const obj = {a:1};
foo(obj)

Updating object values in JavaScript via function [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 9 years ago.
In JavaScript I am trying to update an object value through a function, through which I am passing the object property to update.
However, this won't work - and I can see why, but don't know how to combat it!
myObject = {"testItem": "testValue"};
console.log(myObject.testItem);
function updateSomeValue(objectItem, newValue){
myObject.objectItem = newValue;
}
updateSomeValue('testItem', 'newValue');
console.log(myObject.testItem);
Now, I can see the issue here is that in the function, myObject.objectItem is expecting an item in the object called objectItem - it won't translate it to testItem.
How do I do this?
By using a different notation. Using [ .. ] you can specify the property name as a string.
function updateSomeValue(objectItem, newValue){
myObject[objectItem] = newValue;
}

Categories

Resources