如何获取类对象的名称作为字符串在Javascript? [英] How to get class object's name as a string in Javascript?

查看:126
本文介绍了如何获取类对象的名称作为字符串在Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我在Javascript中实例化一个对象,像这样:

  var myObj = new someObject 

现在,可以获取var对象的名称为string 'myObj






其他详细信息(编辑): h3>

我想要获取保存对象引用的变量名称的原因是我的新 myObj 在需要调用函数 myObj.someFunction()的页面上创建一个新的可点击 DIV 。当我插入新的 DIV 时,我需要知道保存对象引用的变量的名称。可能有更好的方法吗?






你说得对,对不起。 >

我想要获取保存对象引用的变量名称的原因是我的新myObj将在页面上创建一个新的可点击DIV,需要调用function myObj.someFunction()。当我插入新的DIV我需要知道的变量的名称持有对对象的引用。可能有更好的方法吗?

解决方案

Shog9是正确的, ,因为一个对象可以被多个变量引用。如果你真的不在乎,并且你想要的是找到引用该对象的全局变量的名称,你可以做如下hack:

  function myClass(){
this.myName = function(){
//通过全局对象搜索解析为此对象的名称
for(var name in this.global)
if(this.global [name] == this)
return name
}
}
// store在我们的原型的
//属性中的全局对象(在顶层可以称为this),所以我们可以在对象的方法中引用它
myClass.prototype.global = this
//创建一个引用对象的全局变量
var myVar = new myClass()
myVar.myName()//返回myVar

请注意,这是一个丑陋的黑客,不应该在生产代码中使用。如果有多个变量引用一个对象,你不能知道你会得到哪一个。它将仅搜索全局变量,因此如果变量是函数的局部变量,它将不工作。



编辑:若要回应,请在建立时传回建构函式中的名称。你的澄清,如果你需要能够引用来自事件处理程序的东西,你不应该通过名称来引用它,而是添加一个直接引用对象的函数。这里有一个快速的例子,我鞭打了,显示类似,我想,你想要做什么:

  function myConstructor(){
this.count = 0
this.clickme = function(){
this.count + = 1
alert(this.count)
}

var newDiv = document.createElement(div)
var contents = document.createTextNode(Click me!)

//这是关键部分。我们不通过创建一个
//字符串来构造onclick处理程序,而是传递一个函数来完成我们想要的。为了
//引用对象,我们不能直接使用它(因为在运行事件处理程序时会引用
// div),但是我们创建一个匿名函数, b $ b //参数,并传递这个参数。
newDiv.onclick =(function(obj){
return function(){
obj.clickme()
}
})(this)

newDiv.appendChild(contents)
document.getElementById(frobnozzle)appendChild(newDiv)

}
window.onload = function(){
var myVar = new myConstructor()
}


Let's say I instantiate an object in Javascript like this:

var myObj = new someObject();

Now, is it possible to obtain the var object's name as string 'myObj' from within one of the class methods?


Additional details (edited):

The reason why I would like to get the name of the variable holding reference to the object is that my new myObj would create a new clickable DIV on the page that would need to call a function myObj.someFunction(). As I insert the new DIV I need to know the name of the variable holding reference to the object. Is there maybe a better way of doing this?


You are right, sorry for the mixup in terminology.

The reason why I would like to get the name of the variable holding reference to the object is that my new myObj would create a new clickable DIV on the page that would need to call a function myObj.someFunction(). As I insert the new DIV I need to know the name of the variable holding reference to the object. Is there maybe a better way of doing this?

解决方案

Shog9 is right that this doesn't make all that much sense to ask, since an object could be referred to by multiple variables. If you don't really care about that, and all you want is to find the name of one of the global variables that refers to that object, you could do the following hack:

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 
  } 
}
// 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
// create a global variable referring to an object
var myVar = new myClass()
myVar.myName() // returns "myVar"

Note that this is an ugly hack, and should not be used in production code. If there is more than one variable referring to an object, you can't tell which one you'll get. It will only search the global variables, so it won't work if a variable is local to a function. In general, if you need to name something, you should pass the name in to the constructor when you create it.

edit: To respond to your clarification, if you need to be able to refer to something from an event handler, you shouldn't be referring to it by name, but instead add a function that refers to the object directly. Here's a quick example that I whipped up that shows something similar, I think, to what you're trying to do:

function myConstructor () {
  this.count = 0
  this.clickme = function () {
    this.count += 1
    alert(this.count)
  }

  var newDiv = document.createElement("div")
  var contents = document.createTextNode("Click me!")

  // This is the crucial part. We don't construct an onclick handler by creating a
  // string, but instead we pass in a function that does what we want. In order to
  // refer to the object, we can't use this directly (since that will refer to the 
  // div when running event handler), but we create an anonymous function with an 
  // argument and pass this in as that argument.
  newDiv.onclick = (function (obj) { 
    return function () {
      obj.clickme()
    }
  })(this)

  newDiv.appendChild(contents)
  document.getElementById("frobnozzle").appendChild(newDiv)

}
window.onload = function () {
  var myVar = new myConstructor()
}

这篇关于如何获取类对象的名称作为字符串在Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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