[英]Use string to access variable in javascript
If I have: 如果我有:
name.sub = function() {
var sub = {};
var placeholder = "test"
var test = function() {
return 42;
};
// Desired code would be here
return sub;
};
I want to use a placeholder
to access the variable so that I get 42
. 我想使用一个
placeholder
来访问变量,以便得到42
。
Something like window["name"]["sub"][placeholder]
is seemingly looking for name.sub.test. 像
window["name"]["sub"][placeholder]
的东西似乎正在寻找name.sub.test。
The only answers I found were if it was a global variable. 我发现的唯一答案是它是否是全局变量。 Using eval would work, but I've heard it should be avoided where possible.
使用eval可以工作,但我听说应该尽可能避免使用它。
placeholder = "test";
console.log(eval(placeholder + '()'))
// Would return 42
My actual end goal is to have an associative array where: 我的实际最终目标是拥有一个关联数组,其中:
console.log(array[placeholder]);
// Would return 42
Any help would be appreciated. 任何帮助,将不胜感激。
This is what I ended up using for anyone interested: 这是我最终对感兴趣的任何人使用的:
name.sub= function() {
var sub = {};
var placeholder = "test"
var test = function() {
return 42;
var newObj = {};
newObj["test"] = function() {test()}
console.log(newObj[placeholder]())
// Should return 42
};
You can't access variables inside a function from outside said function. 您不能从所述函数外部访问函数内部的变量。
Instead, you could do this: 相反,您可以这样做:
name.sub = function(placeholder) {
var functions = {
"test": function() {
return 42;
},
};
return functions[placeholder]();
};
name.sub("test"); // 42
I'm not sure if that's what you're looking for but hopefully it is. 我不确定这是否是您要找的东西,但希望如此。 Explain more?
解释更多?
You can't access local variables inside a function the same way you can access properties of window
in global scope. 您不能以与访问全局范围内的
window
属性相同的方式访问函数内部的局部变量。
kangax wrote an interesting article about Understanding delete [in JavaScript] , which includes an explanation of what Activation objects are - which I think is what you're looking for. kangax写了一篇有趣的文章[了解JavaScript中的delete] ,其中包括对什么是激活对象的解释-我认为这是您正在寻找的对象。
I suggest you read the entire article, but long story short: 我建议您阅读全文,但长话短说:
Inside functions, declared variables (and functions) are added as properties to the activation object of the current scope, as they get added to window
in the global scope. 在函数内部,声明的变量(和函数)作为属性添加到当前范围的激活对象中,因为它们被添加到全局范围的
window
中。
But unlike window
: 但是与
window
不同:
Note that Activation object is an internal mechanism and is never really accessible by program code.
请注意,激活对象是一种内部机制,程序代码永远无法真正访问它。
Conclusion: What you're asking is not (currently) possible. 结论:您要的(当前)不可能。
Your options are limited to: 您的选择仅限于:
Using an intermediate object: 使用中间对象:
name.sub = function() { var sub = {}; var placeholder = "test"; var array = {}; array.test = function() { return 42; }; console.log(array[placeholder]()); return sub; };
Using eval, exactly like you suggested: 使用eval,完全像您建议的那样:
name.sub = function() { var sub = {}; var placeholder = "test"; var test = function() { return 42; }; console.log(eval(placeholder + '()')); return sub; };
Using window
, by removing var
from test
's declaration: 通过使用
window
,通过从test
的声明中删除var
:
name.sub = function() { var sub = {}; var placeholder = "test"; test = function() { return 42; }; console.log(window[placeholder]()); return sub; };
I suggest the first option for the sake of performance over eval
, for compatibility over window
(might collide with other code), and simply because of personal taste and what I consider good practice. 我建议使用第一个选项,是因为
eval
更好的性能,与window
兼容性(可能与其他代码冲突),并且仅仅是出于个人喜好和我认为的良好做法。
By what i understand of your question, 根据我对您的问题的了解,
its seem like you are looking for a key/value pair to a JavaScript object literal, 似乎您正在寻找JavaScript对象文字的键/值对,
window.name is reserved btw: https://developer.mozilla.org/en-US/docs/Web/API/Window/name 保留window.name,顺便说一句: https : //developer.mozilla.org/zh-CN/docs/Web/API/Window/name
var sub = {
'test': function() {
return 42;
},
'test2': 42;
}
sub['test']();
sub['test2'];
add by 加上
Using dot notation:
sub.test3 = "value3";
Using square bracket notation:
sub["test4"] = "value4";
Maybe im thinking to simple , but seems like this is what you are looking for 也许我在想简单,但似乎这就是您想要的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.