Pass window variable as parameter in Javascript [duplicate] - javascript

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

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)

Javascript var reference. Could someone explains to me what's the difference between primitive and objs/array? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Does JavaScript pass by reference? [duplicate]
(13 answers)
Closed 8 years ago.
I'd like understand why this difference (I suppose works like Java, so it's stack and heap difference)
var a = 10;
console.log(a);//10
function ChangeVal(){ b=a; b++; }
console.log(a);//10
var a = {name:"MyName"};
console.log(a);//{name:"MyName"}
function ChangeVal(){ b=a; b.name = "YourName"; }
console.log(a);//{name:"YourName"}
Assigning the value of one variable to another always involves copying the value. Thus:
b = a;
assigns the value of variable "a" to the (global! you forgot var!) variable "b". That happens in both your examples.
In the first example, the value of "a" is the number 10. In the second, it's a reference to an object. After
b = a;
in both cases, the variable "b" has the same value — a copy of the same value — as "a".
Because one reference to a particular object is as good as another, in your second example it's perfectly natural that changing the value of the "name" property on that object can be done via the reference in either "a" or "b".

Do Javascript variable assignments work by reference? [duplicate]

This question already has answers here:
JavaScript by reference vs. by value [duplicate]
(4 answers)
Closed 8 years ago.
I have a question about referencing objects in javascript.
Say I have a variable that is some object (lets say json) and it is called objOne - (var objOne = someJSONObject;).
If I go ahead and declare
var objTwo = objOne;
will I have two references to the same Object? Kind of like a c pointer?
To sum it up :
assignements are done by value
you never manipulate objects, only object references
This means that
you'll have two references to the same object (you can check that by changing a property of the object)
when you're passed in a variable the value of a primitive, changing your variable doesn't change other variables
EDIT : as it's a duplicate I'll delete this answer in a minute to allow a proper closing if there's no other answer. Please vote to close.
Yes, objects are passed by reference:
function changeVal(obj){
obj.value = "bar"
}
(function checkRefs(){
var myObject = {
value: "foo"
};
alert(myObject.value);
changeVal(myObject);
alert(myObject.value);
})();

Get the name property of a variable [duplicate]

This question already has answers here:
Get variable name. javascript "reflection"
(4 answers)
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 8 years ago.
I was wondering if it's possible to get the name of a variables in javascript or JQuery.
Suppose that I declare a variable in javascript like:
var customerNr = "456910";
If a function receive a variable, how can i return the name of the variable?
For example:
function getNameOfVariable(someVariable){
//return name of someVariable;
}
If I call this function like this:
getNameOfVariable(customerNr);
The function has to return a string whose value is "customerNr".
How can I do this in jquery or javascript? Some kind of reflection?
That is simply not possible!
The passed parameter doesn't even have to have a name. It could be a return value of a function or a raw literal (like "string" or 3).
No, this is not possible. Only values are transferred for primitive data types, not references, so how would you know what the "name" of the variable is? For example:
var customerNr="456910";
var customerNrAfterProcessing=customerNr;
getNameOfVariable(customerNrAfterProcessing);
Would it return customerNrAfterProcessing or customerNr?
However, you can imitate this behavior with objects.
var variableName = someMethodThatDeterminesVariableNameOrJustAConstantIfYouPrefer();
var myVariables={};
var myVariables[variableName]={};
myVariables[variableName].value="456910";
myVariables[variableName].variableName=variableName;
function getNameOfVariable(someVariable){
return someVariable.variableName;
}
getNameOfVariable(myVariables[variableName]);
Obviously, this amounts to a lot more work than just using the variableName variable directly, but it could be necessary in some more complicated situations.
See working with objects in JS reference

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