最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Reference a Javascript variable by a text alias - Stack Overflow

programmeradmin5浏览0评论

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".

Share Improve this question edited Jan 28, 2014 at 8:18 Grijesh Chauhan 58.3k20 gold badges143 silver badges213 bronze badges asked Jan 28, 2014 at 7:36 JohannJohann 29.9k43 gold badges188 silver badges317 bronze badges 2
  • there is eval() in JavaScript that evaluates x and assign value to y – 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
Add a ment  | 

6 Answers 6

Reset to default 4

if 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   
发布评论

评论列表(0)

  1. 暂无评论