Javascript - 使用字符串连接设置变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19697758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - set a variable using concatenation of strings
提问by Chris Schmitz
Is it possible to set a variable by concatenating two strings together to form the name?
是否可以通过将两个字符串连接在一起形成名称来设置变量?
If at all possible I'd like to determine what variable to set based on the class names of the objects that the user clicks. I know I can hard code a bunch of if/else if statements, but it would be really cool if I could reference the variables indirectly. I was thinking something like this:
如果可能的话,我想根据用户单击的对象的类名确定要设置的变量。我知道我可以对一堆 if/else if 语句进行硬编码,但是如果我可以间接引用这些变量,那就太酷了。我在想这样的事情:
var owner_read;
var group_read;
function setVariableIndirectly(object){
var second = object.className; // returns "read"
var first = object.parentElement.className; // returns "group"
first + "_" + second = "set this as the new variable";
}
Is there any way of doing this??
有没有办法做到这一点?
EDIT:
编辑:
Here's the html that the data is coming in from.
这是数据来自的 html。
<p class="owner">
<span class="read" onclick="permissionClick(this)">r</span>
<span class="write" onclick="permissionClick(this)">w</span>
<span class="execute" onclick="permissionClick(this)">x</span>
</p>
采纳答案by jfriend00
It's not clear exactly what you're trying to accomplish, but you can access variables by name as properties of an object.
目前还不清楚您要完成什么,但您可以通过名称访问变量作为对象的属性。
// this is the container to hold your named variables
// (which will be properties of this object)
var container = {};
function setVariableIndirectly(obj){
var second = obj.className; // returns "read"
var first = obj.parentNode.className; // returns "group"
// this is how you access a property of an object
// using a string as the property name
container[first + "_" + second] = "set this as the new variable";
// in your example container["read_group"] would now be set
}
It's probably better to put your variables on your own container object as shown above, but you can also access global variables via properties on the window
object.
如上所示,将变量放在自己的容器对象上可能更好,但您也可以通过window
对象上的属性访问全局变量。
回答by Brian Peacock
This is possible but you have to be wary of context and scope.
这是可能的,但您必须注意上下文和范围。
1. To set variable with global scope in browser environment:
1. 要在浏览器环境中设置具有全局作用域的变量:
window[str1 + str2] = value
2. To set variable with global scope in node environment:
2.在节点环境中设置具有全局作用域的变量:
global[str1 + str2] = value
3. Within a closure and scoped within that closure:
3. 在一个闭包内并在该闭包内限定范围:
this[str1 + str2] = value
Within the closure, global and window will still set the global. Note that if you are within a function that is being called, 'this' could refer to another object.
在闭包内, global 和 window 仍然会设置 global。请注意,如果您在被调用的函数中,“this”可能会引用另一个对象。
回答by Gaurav
You can set a global variable this way:
您可以通过以下方式设置全局变量:
window[first + "_" + second] = "set this as the new variable";
and access it as:
并将其访问为:
console.log(group_read);