I have a variable myVariable
which is continuously changing. At some point I want to capture the value of myVariable
(not a reference to myVariable
) into another variable myVariableAtSomePoint
.
Example code:
var myVariable = 1;
function test () {
var myVariableAtSomePoint= myVariable;
console.log(myVariableAtSomePoint);
}
myVariable = 2;
test(); // Prints 2, instead of 1.
run(init) v = sess. run(x) print(v) # will show you your variable. The part with init = global_variables_initializer() is important and should be done in order to initialize variables. Also, take a look at InteractiveSession if you work in IPython.
You store a value in a variable by putting the variable name on the left side of an assignment statement.
currentValue. The value of the current element. On first call, the value of array[0] if an initialValue was specified, otherwise the value of array[1] . currentIndex.
You can "assign a new value" to your variable, but you can't change the value itself, unlike an array or object, which you can modify without affecting the variable binding.
You mention in your comments that myVariable
is a number. Since myVariable
houses a primitive type, simply use the code below:
myVariableAtSomePoint = myVariable;
Take a look at JAVASCRIPT: PASSING BY VALUE OR BY REFERENCE. Here's a quote:
When passing in a primitive type variable like a string or a number, the value is passed in by value.
I would also suggest reading: How do I correctly clone a JavaScript object?
EDIT:
I believe that you're assuming that the placement of the function in the code affects the value of the variables. It does not. See examples below:
This:
function test () {
var myVariableAtSomePoint= myVariable;
console.log(myVariableAtSomePoint);
}
myVariable = 2;
test(); // Prints 2, instead of 1.
Is the same as this:
var myVariable = 1;
function test () {
var myVariableAtSomePoint= myVariable;
console.log(myVariableAtSomePoint);
}
myVariable = 2;
test(); // Prints 2, instead of 1.
Your problem is that you're changing the value of myVariable
before you're assigning it to myVariableAtSomePoint
. For this to work as you want, you'll need to call the test()
function before you change the value of myVariable
var myVariable = 1;
function test () {
var myVariableAtSomePoint= myVariable;
console.log(myVariableAtSomePoint);
}
test(); // Prints 1
myVariable = 2;
test(); // Prints 2
IMPORTANT: No matter the placement of the function, the code inside test()
is not executed until you call the function.
The variable only holds the reference and nothing else. If you want a copy of the object that the variable is pointing to, you will need some way to copy that object.
Either you can implement a copy method on the object itself (if you know what properties it has) or you can iterate through all properties on the object using a for ... in
loop and copy them to a newly allocated object.
Something like:
//o is the object you want to copy
var o2 = {}; //gives a new object
for (x in o) {
o2[x] = o[x];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With