JavaScript:在 IE 中列出全局变量

标签 javascript html internet-explorer

我正在尝试获取我的类的实例名称。
我这样做的方法是遍历所有全局对象并将其与 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"

最佳答案

更好的主意,已解决:

        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"

关于JavaScript:在 IE 中列出全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2462199/

相关文章:

javascript - 如何根据查询结果 "highlight"一个div?

javascript - CreateJS 中的补间函数

html - 基于图像的链接上的工具提示和自定义光标问题

javascript - 如何知道 javascript 函数被调用了?

javascript - jQuery 在不使用 href 的情况下平滑滚动

javascript - codepen 上有一个作品是用三个 js 做的,我们想改变它的颜色

html - 单击后按钮仍保持按下状态

javascript - 如何使用 JavaScript 更改 CSS 属性

javascript - 尝试导航到另一个页面时 IE7 和 IE8 中出现 "Stop running this script"错误

html - 如何确保 CSS 反转的按钮间距均匀?