Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

html - JavaScript: List global variables in IE

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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...