简体   繁体   English

从变量设置javascript变量名

[英]setting javascript variable name from a variable

Hi is it possible to set a var name from another variable? 您可以从另一个变量设置var名称吗?

Such as I have the var test with different numbers each time the function is called so I want the number to be a variable name eg var test contains ' 1 ' so the new variable would be var 1 . 比如我每次调用函数时都有不同数字的var test ,所以我希望数字是一个变量名,例如var test包含'1',所以新变量将是var 1 I then want to add a word to this number "word" to make the new variable become var 1word . 然后,我想在这个数字“单词”中添加一个单词,使新变量成为var 1word

var newNumber = 1;
var newNumber += 'word';
       ^ becomes ' 1 '
the new var name becomes ' 1word '

edit: 编辑:

function getNumber(newNumber) //  Gets a number, for this example say its the number '1' 
    {

        var newNumber += "something";  //The new varaible will be named "1something" (something can be any word)

    }

Hope I haven't made it sound too confusing, thanks. 希望我没有让它听起来太混乱,谢谢。

You can put the variables in an object and index the object using string keys. 您可以将变量放在对象中,并使用字符串键索引对象。

For example: 例如:

var myData = { };
myData.x1 = 42;
alert(myData["x" + 1]);

You can also initialize the object using object notation: 您还可以使用对象表示法初始化对象:

var myData = { key: 42 };

Everything in Javascript is basically just an associative array. Javascript中的所有内容基本上只是一个关联数组。 This means you can use array access notation and dot notation more or less interchangeably. 这意味着您可以或多或少地交替使用数组访问符号和点符号。 Array access notation provides the feature you are looking for: 数组访问符号提供您正在寻找的功能:

object["name"]="foo"

will set a name property on the given object. 将在给定对象上设置name属性。

If you want to create a global variable then use the window object 如果要创建全局变量,请使用window对象

window["name"]="foo"

You can also use an expression or a variable in the square brackets 您还可以在方括号中使用表达式或变量

window[someVaribale]="foo";
window["name_" + someVariable]="foo";

Far as I can understand, you want to create a variable that's named after the contents of another variable. 我可以理解,你想创建一个以另一个变量的内容命名的变量。

Not sure if you can do this directly, but you can do it pretty painlessly using objects. 不确定你是否可以直接这样做,但你可以非常轻松地使用对象。

var obj = new Object();
obj['original'] = 'name';
obj[obj]['original']] = 'another name';

The object now has this structure: 该对象现在具有以下结构:

{
  'original': "name",
  'name': "another name" #variable named after original's contents
}

There's a simple way to achieve this, if you store all your variables inside an object/array. 如果将所有变量存储在对象/数组中,则有一种简单的方法可以实现此目的。 Remember, in javascript, arrays are objects, objects are objects etc :p 请记住,在javascript中,数组是对象,对象是对象等:p

var mystuff = {}; // new object literal

var numVariables = 10;

for (var i = numVariables - 1; i >= 0; i--){
    var newkey          = i + "_my_index";
    mystuff[newkey]     = "some stuff";
}

If you're using numeric "variables" like in your exemple (var 1; var 2;) you should use Array; 如果你在例子中使用数字“变量”(var 1; var 2;),你应该使用Array;

var list=[]; //declaration

list[1]="something"; //create a "variable" 1 with 'something' inside

list[2]="other thing"; //create another variable

var name=3;
var value="Hello";

list[name]=value; //works too

alert( list[1] ); //something

if you're using words as "keywords" you should use objects instead of Arrays 如果你使用单词作为“关键字”,你应该使用对象而不是数组

var list={}; //declaration

list["car1"]="Honda";
list["car2"]="Toyota";
list["fuit"]="Apple";

var name="greeting";
var value="Hello";

list[name]=value; //works too

alert( list["car2"] ); //Toyota

hope you understood. 希望你明白。

We need more of an explanation of what you are trying to accomplish. 我们需要更多地解释你想要完成的事情。 This looks like a really bad idea. 这看起来真的很糟糕。 Most likely you really should just used a named index array. 很可能你真的应该只使用一个命名的索引数组。 Eg. 例如。

var stuff = {}
stuff[1] = "one";
stuff["name"] = "david";

I'm confused by your question but it seems like you want to change a variables name which cannot be done. 我对您的问题感到困惑,但似乎您想要更改无法完成的变量名称。 You can create a new variable with the desired name and assign it whatever value you want. 您可以使用所需名称创建一个新变量,并为其指定所需的任何值。

My question is why would you want to do this? 我的问题是你为什么要这样做?

You may be able to achieve this with the builtin method: eval( "your javascript code here" ); 您可以使用内置方法实现此目的:eval(“您的javascript代码在这里”);

so maybe in your case it would be something like: eval( "var yourNewName = \\"" + yourOldVarValue + "\\""); 所以也许在你的情况下它会是这样的:eval(“var yourNewName = \\”“+ yourOldVarValue +”\\“”); then nullify your old variable. 然后使旧变量无效。

But as the others express, I can't see why or in which case you would want to rename a variable (unless its for some weird hacking trickery lol). 但正如其他人所表达的那样,我看不出为什么或者在哪种情况下你想要重命名一个变量(除非它对于一些奇怪的黑客技巧大声笑)。

[not tested, so this eval may not even work :S] [没有经过测试,所以这个评估可能不起作用:S]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM