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

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

问题描述

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

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

var myObj = new someObject();

现在,是否可以从类方法之一中以字符串 'myObj' 的形式获取 var 对象的名称?

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

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

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.

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

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 是对的,这个问题没有多大意义,因为一个对象可以被多个变量引用.如果您并不真正关心这一点,而您只想找到引用该对象的全局变量之一的名称,则可以执行以下操作:

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"

请注意,这是一个丑陋的 hack,不应在生产代码中使用.如果引用一个对象的变量不止一个,你就分不清会得到哪一个.它只会搜索全局变量,因此如果变量是函数的局部变量,它将不起作用.一般来说,如果你需要命名一些东西,你应该在创建它时将名称传递给构造函数.

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:为了回应您的澄清,如果您需要能够从事件处理程序中引用某些内容,则不应按名称引用它,而是添加一个函数直接指向对象.这是我快速创建的一个示例,我认为它显示了与您正在尝试执行的操作类似的内容:

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天全站免登陆