我想找出本地变量中是否存在以给定名称作为字符串的变量。有没有办法做这样的事情?
var name = 'myVariable';
function test(myVariable) {
//CHECK HERE if there is a locally scoped variable with the same name
//as the value of 'name'
}
我会piggy带Bandrami的回答和我的评论,因为我相信它是完成您尝试做的事情的唯一方法。是的,我知道我正在使用eval
,但是我不相信有任何选择。
if (typeof eval(name) !== 'undefined' && typeof window[name] === 'undefined'){
// variable exists in this scope
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句