Is it possible to reference a JavaScript variable by a text alias? For example:
var x = 2;
var y = convertToVariableRef("x");
After calling the above function:
"y
would be the same reference as x
and not just simply copying the value of x
into y
".
Is it possible to reference a JavaScript variable by a text alias? For example:
var x = 2;
var y = convertToVariableRef("x");
After calling the above function:
"y
would be the same reference as x
and not just simply copying the value of x
into y
".
-
there is
eval()
in JavaScript that evaluatesx
and assign value toy
– Grijesh Chauhan Commented Jan 28, 2014 at 7:41 - 1 possible duplicate of javascript variable reference/alias – Ashoka Mondal Commented Jan 28, 2014 at 7:49
6 Answers
Reset to default 4if you declare an object with out any scope of function its a property of window object, so you can get it reference like this
function convertToVariableRef (ref) {
return window[ref];
}
var x = 2;
var y = convertToVariableRef("x");
but it just copy the value for primitives , and reference only for non-primitives.
array,objects etc are non-primitives.
var x = [1];
var y = convertToVariableRef("x");
y[0] = 2;
// log: x --> [2]
You could go right on and create an Object reflect and assign properties to it.
var reflect = new Object();
reflect.x = 2;
var y = reflect["x"];
Fiddle: http://jsfiddle/wE4Ft/
Referencing a primitive variable such as an integer is not possible. If you really want it you could have some listeners watching your value changes by adding some extra plex padding like some frameworks do (such as AngularJS).
But actually, it would be way simpler to wrap your value into an object, which is referentiable, ie:
var x = {value: 2};
var y = x;
Just use x.value
or y.value
then.
After calling the above function, y would be the same reference as x and not just simply copying the value of x into y.
No, JavaScript doesn't have references to variables in that sense.
You're better off using an object and a property:
var obj = {x: 2};
var yobj = obj;
consoel.log(yobj.x); // 2
yobj
and obj
both refer to the same object in memory in the above, and that object has an x
property. So modifying x
through either reference updates that one object.
Why I said "JavaScript doesn't have references to variables in that sense" above: JavaScript's closures receive an implicit reference to a hidden object, called the variable binding object, that in turn refers to variables. So in that sense, JavaScript has references to variables (indirectly, through a hidden object). But you can't get a reference to that hidden object, so it doesn't really help with what you described.
1) If the value of the variable is a primitive (number, bool, string), you cant get a reference to it. You can only copy it.
2) Even if the variable is NOT a primitive, but is attached to the current scope (i.e., declared with var, like in your example) it's impossible (with two exceptions).
The case that would work is hence a non-primitive, that's part of some other object than the current scope. Like this:
var obj = { a: [1,2,3], b: 42 };
var copyOfA = obj.a;
// do something to copyOfA
copyOfA.splice(1);
// now this has changed!
console.log(obj.a);
The two exceptions are:
- Using eval (which is not very idiomatic)
- If the current scope is the global scope (which it most often isn't, since you're not making everything global, RIGHT?)
For objects and arrays, two variables can point to the same object or array, but for simple values like numbers or booleans, there is no way in javascript to have two variables pointing to the same primitive value.
You can wrap a primitive value into an object and solve the problem that way.
var x = {val: 2};
var y = x; // now both y and x point to the same object
x.val = 3;
console.log(x.val); // 3
console.log(y.val); // 3 (same value)
And, with property names, you can also use the string name to access the property as in:
var x = {val: 2};
var y = x; // now both y and x point to the same object
console.log(y["val"]); // 2