JavaScript:在IE中列出全局变量

JavaScript:在IE中列出全局变量,javascript,html,internet-explorer,Javascript,Html,Internet Explorer,我正在尝试获取类的实例名。 我这样做的方式是循环所有全局对象,并将其和这个指针进行比较。 它可以在Chrome和FF中工作,但在IE中则不行。问题似乎是全局变量似乎不在窗口中。 我如何在IE中循环全局变量? PS:我知道它只在只有一个实例的情况下工作,我不想将实例的名称作为参数传递 function myClass() { this.myName = function () {

我正在尝试获取类的实例名。
我这样做的方式是循环所有全局对象,并将其和这个指针进行比较。
它可以在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"
更好的主意,解决了:

        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"
函数myClass_IE()
{ 
this.myName=函数()
{ 
//在全局对象中搜索解析为此对象的名称
对于(var i=0;i
在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从缓存(或从服务器(如果未缓存)获取内容的外部脚本。

您可以提供更多上下文吗。为什么要这样做?获取类的实例名