本文介绍了获取所有 Javascript 变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
javascript 有没有办法检测所有分配的变量?例如,如果一个 js 文件创建了一堆 vars(全局范围),那么后续文件是否可以在不知道它们的名称以及可能存在的情况下获取所有 vars?
Is there a way for javascript to detect all assigned variables? For example, if one js file creates a bunch of vars (globally scoped), can a subsequent file get all the vars without knowing what they're named and which might exist?
提前致谢:)
编辑,问题第 2 部分:
EDIT, Question Part 2:
如何获取这些变量的值?这是我尝试过的:
How do I get the values of these variables? Here is what I have attempted:
根据评论建议,这就是我最终得到的结果:
This is what I ended up with, as per comment suggestions:
for (var name in this) {
variables[name] = name;
variables[name]=this[name]
}
推荐答案
Flanagan 的JavaScript - The Definitive Guide"在第 653 页给出了以下内容:
Flanagan's "JavaScript - The Definitive Guide" gives the following on page 653:
var variables = ""
for (var name in this)
variables += name + "
";
这篇关于获取所有 Javascript 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!