2010-03-17 131 views
3

我试图获取我的类的实例名称。
我这样做的方式是循环遍历所有全局对象并将其与此指针进行比较。
它在Chrome和FF中工作,但在IE中,它没有。这个问题似乎是全球变量似乎并不在窗口中。
如何循环浏览IE中的全局变量?

PS:我知道它只在只有一个实例时才起作用,而且我不想将实例的名称作为参数传递。JavaScript:在IE中列出全局变量

 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" 
+0

你能否提供更多的上下文。你为什么做这个? – Cheeso 2010-03-17 12:42:58

+0

获取类的实例名称 – 2010-03-17 13:02:47

回答

2

更好的主意,解决了:

 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" 
2

在IE浏览器,它的全局变量是不可枚举的,除非你明确地将它们定义为window对象的属性。

var noEnum = true;  // won't show up in a for...in loop 
window.willEnum = true; // will show up in a for...in loop 

显然,你找到了自己的解决方案,但它会为内联脚本才有效 - 尽管这可以扩展到使用AJAX来从缓存中的内容(或从服务器,如果他们不是外部脚本缓存)。