Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中使用动态变量名称(Use dynamic variable names in JavaScript)

In PHP you can do amazing/horrendous things like this:(在PHP中,您可以执行以下令人惊奇/可怕的事情:)

$a = 1;
$b = 2;
$c = 3;
$name = 'a';
echo $$name;
// prints 1

Is there any way of doing something like this with Javascript?(有没有办法用Java做类似的事情?)

Eg if I have a var name = 'the name of the variable';(例如,如果我有一个var name = 'the name of the variable';)

can I get a reference to the variable with name name ?(如何获得名称为name的变量的引用?)   ask by Finbarr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since ECMA-/Javascript is all about Objects and Contexts (which, are also somekind of Object), every variable is stored in a such called Variable- (or in case of a Function, Activation Object ).(由于ECMA // Javascript都是关于ObjectsContexts (它们也是某种对象)的,因此每个变量都存储在所谓的Variable- (如果是Function,则为Activation Object )中。)

So if you create variables like this:(因此,如果您创建这样的变量:)

var a = 1,
    b = 2,
    c = 3;

In the Global scope (= NO function context), you implicitly write those variables into the Global object (= window in a browser).(在全局范围 (= NO函数上下文)中,您将这些变量隐式写入全局对象 (=浏览器中的window )。)

Those can get accessed by using the "dot" or "bracket" notation:(可以使用“点”或“括号”表示法来访问它们:)

var name = window.a;

or(要么)

var name = window['a'];

This only works for the global object in this particular instance, because the Variable Object of the Global Object is the window object itself.(这仅在此特定实例中适用于全局对象,因为全局对象变量 对象window对象本身。)

Within the Context of a function, you don't have direct access to the Activation Object .(在函数的上下文中,您没有直接访问Activation Object的权限。) For instance:(例如:)
function foobar() {
   this.a = 1;
   this.b = 2;

   var name = window['a']; // === undefined
   alert(name);
   name = this['a']; // === 1
   alert(name);
}

new foobar();

new creates a new instance of a self-defined object (context).(new创建一个自定义对象(上下文)的新实例。)

Without new the scope of the function would be also global (=window).(没有new功能,该功能的范围也将是global (=窗口)。) This example would alert undefined and 1 respectively.(此示例将分别警告undefined1 。) If we would replace this.a = 1; this.b = 2(如果我们将其替换this.a = 1; this.b = 2) this.a = 1; this.b = 2 with:(this.a = 1; this.b = 2其中:)
var a = 1,
    b = 2;

Both alert outputs would be undefined.(两个警报输出均未定义。)

In that scenario, the variables a and b would get stored in the Activation Object from foobar , which we cannot access (of course we could access those directly by calling a and b ).(在这种情况下,变量ab将存储在foobar的激活对象中,我们无法访问它(当然,我们可以通过调用ab直接访问它们)。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...