JavaScript:在 IE 中列出全局变量

Posted

技术标签:

【中文标题】JavaScript:在 IE 中列出全局变量【英文标题】:JavaScript: List global variables in IE 【发布时间】:2010-03-17 12:37:40 【问题描述】:

我正在尝试获取我的班级的实例名称。 我这样做的方法是遍历所有全局对象并将其与 this 指针进行比较。 它适用于 Chrome 和 FF,但在 IE 中则不行。问题似乎是全局变量似乎不在窗口中。 如何循环浏览 IE 中的全局变量? PS:我知道它只有在只有一个实例的情况下才有效,并且我不想将实例的名称作为参数传递。

        function myClass() 
         
            this.myName = function () 
             
                // search through the global object for a name that resolves to this object
                for (var name in this.global) 
                
                    if (this.global[name] == this) 
                        return name 
                
             
        





        function myClass_chrome() 
         
            this.myName = function () 
             
                // search through the global object for a name that resolves to this object
                for (var name in window) 
                
                    if (window[name] == this) 
                        return name ;
                
             ;
        

// store the global object, which can be referred to as this at the top level, in a
// property on our prototype, so we can refer to it in our object's methods
myClass.prototype.global = this
//myClass_IE.prototype.global = this
 // create a global variable referring to an object
// var myVar = new myClass()
var myVar = new myClass_chrome()
//var myVar = new myClass_IE()

 alert(myVar.myName() );// returns "myVar"

【问题讨论】:

您能否提供更多上下文。你为什么要这样做? 获取类的实例名 【参考方案1】:

更好的主意,解决了:

        function myClass_IE() 
         
            this.myName = function () 
             
                // search through the global object for a name that resolves to this object
                for (var i = 0; i < document.scripts.length; i++)
                
                    var src = document.scripts[i].innerhtml ;
                    //document.write('script ' + i + ' = ' + document.scripts[i].innerHTML )


                    var idents = src.replace(/\W/g, ' ').replace(/(function|if|for|while|true|false|null|typeof|var|new|try|catch|return|prototype|this)/g, '').split(' ');
                    for(var j = 0; j < idents.length; j++) 
                    
                        //var iden = String(idents[j]).trim();
                        var iden = String(idents[j]);
                        if (window[iden] == this) 
                        
                            // http://mcarthurgfx.com/blog/article/iterating-global-variables-in-internet-explorer
                            // http://blog.stevenlevithan.com/archives/faster-trim-javascript
                            return iden;
                        
                    
                
             
        





        function myClass() 
         
            this.myName = function () 
             
                // search through the global object for a name that resolves to this object
                for (var name in this.global) 
                
                    if (this.global[name] == this) 
                        return name 
                
             
        





        function myClass_chrome() 
         
            this.myName = function () 
             
                // search through the global object for a name that resolves to this object
                for (var name in window) 
                
                    if (window[name] == this) 
                        return name ;
                    
             ;
        

// store the global object, which can be referred to as this at the top level, in a
// property on our prototype, so we can refer to it in our object's methods
myClass.prototype.global = this
//myClass_IE.prototype.global = this
// create a global variable referring to an object
// var myVar = new myClass()
//var myVar = new myClass_chrome()
var myVar = new myClass_IE()

alert(myVar.myName() );// returns "myVar"

【讨论】:

【参考方案2】:

在 IE 中,它的全局变量是不可枚举的,除非您将它们明确定义为窗口对象的属性。

var noEnum = true;       // won't show up in a for...in loop
window.willEnum = true;  // will show up in a for...in loop

显然,您找到了自己的解决方案,但它仅适用于内联脚本 - 尽管这可以扩展到使用 ajax 从缓存(如果未缓存则从服务器)获取内容的外部脚本。

【讨论】:

以上是关于JavaScript:在 IE 中列出全局变量的主要内容,如果未能解决你的问题,请参考以下文章

javascript如何列出全局对象的非原生属性。

javascript如何列出全局对象的非原生属性。

打印所有全局变量/局部变量?

JavaScript全局变量与局部变量

如何在 Visual Studio 的监视窗口(用于 C++)上列出和查看所有全局变量?

javascript定义了一个全局变量,怎么在局部变量中修改该变量的值?