JavaScript:在IE中列出全局变量 [英] JavaScript: List global variables in IE

查看:98
本文介绍了JavaScript:在IE中列出全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取我班的实例名称。


我这样做的方式是遍历所有全局对象并将其与此指针进行比较。


它可以在Chrome和FF中运行,但在IE中却不行。这个问题似乎是全球变量似乎并不在窗口中。


如何循环浏览IE中的全局变量?




PS:我知道它只有只有一个实例时才有效,而且我不想将该实例的名称作为参数传递。
$ b $ pre $ function myClass()
{
this.myName = function()
{
//在全局对象中搜索解析为这个对象的名字
for(var this this在全局中)
{
if(this.global [name] = = this)
返回名称
}
}
}





函数myClass_chrome ()
{
this.myName = function()
{
//在全局对象中搜索解析为该对象的名称
for(var name在窗口中)
{
if(window [name] == this)
返回名称;
}
};
}

//在我们的原型的
//属性中存储全局对象,它可以被称为顶层,所以我们可以参考它在我们的对象的方法中
myClass.prototype.global = this
//myClass_IE.prototype.global = this
//创建一个引用对象的全局变量
// var myVar = new myClass()
var myVar = new myClass_chrome()
// var myVar = new myClass_IE()

alert(myVar.myName()); //返回myVar


解决方案

>

 函数myClass_IE()
{
this.myName = function()
{
//在全局对象中查找解析为该对象的名称
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,'')。分裂(' ');
for(var j = 0; 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;
}
}
}
}
}





函数myClass()
{
this.myName = function()
{
//在全局对象中搜索解析为该对象的名称
for(var (this.global中的名称)
{
if(this.global [name] == this)
返回名称
}
}
}





函数myClass_chrome()
{
this.myName = function()
{
//在全局对象中查找解析为这个对象的名称
for(var name in window)
{
if(window [name] == this)
返回名称;
}
};
}

//在我们的原型的
//属性中存储全局对象,它可以被称为顶层,所以我们可以参考它在我们的对象的方法中
myClass.prototype.global = this
//myClass_IE.prototype.global = this
//创建一个引用对象的全局变量
// var myVar = new myClass()
// var myVar = new myClass_chrome()
var myVar = new myClass_IE()

alert(myVar.myName()); //返回myVar


I'm trying to get the instance name of my class.
The way I do this is I loop through all global objects and compare it with the this pointer.
It works in Chrome and FF, but in IE, it doesn't. The problem seems to be the global variables don't seem to be in window.
How can I loop through the global variables in IE ?

PS: I know it only works as long as there is only one instance, and I don't want to pass the instance's name as a parameter.

        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"

解决方案

Better idea, solved:

        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中列出全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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