获取所有的Javascript变量? [英] Get all Javascript Variables?

查看:76
本文介绍了获取所有的Javascript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让JavaScript检测所有分配的变量?例如,如果一个js文件创建了一堆vars(全局作用域),后续文件可以获得所有vars而不知道它们是什么名称以及哪些可能存在?



在此先感谢:)

编辑,问题第2部分:



如何获取这些变量?这是我的尝试:

这是我结束了,根据评论意见:

 for(var name in this){
variables [name] = name;
variables [name] = this [name]
}


解决方案Flanagan的JavaScript - 权威指南在第653页给出了以下内容:

  var variables =


for(var name in this)
variables + = name +\\\
;


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?

Thanks in advance :)

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's "JavaScript - The Definitive Guide" gives the following on page 653:

var variables = ""
for (var name in this)
    variables += name + "\n";

这篇关于获取所有的Javascript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆