0

Like, I've a condition like this:

var a="avar", b=3;
var a+b="some value";
// expecting to make, avar3="some value";

Is it possible somehow? Or any alternative way?

4

4 回答 4

5

You can do:

var a="avar", b=3;
window[a+b]="some value";

This will declare a global variable avar3 with value "some value"

于 2013-08-29T06:40:40.060 回答
2

This:

function ns() { return this; }

var a="avar", b=3;
ns()[a+b] = "some value";

alert(avar3);

will create variable with the name "avar3" in current namespace.

于 2013-08-29T06:45:39.407 回答
1

No Doesn't work like that.

i dont know if this if what you're trying to do but:

var c=a+b; c="some value";

于 2013-08-29T06:43:00.723 回答
0

you can use object properties for something like this

 var obj = { a: 'avar', b:'3'};
 obj[obj.a+obj.b]='some value';
 console.log(obj.avar3);  //the output is 'some value'

http://jsfiddle.net/fGaFJ/

于 2013-08-29T06:42:25.807 回答