Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...