【问题标题】:How to obtain the variable name which holds an object instance from within the object : Javascript如何从对象中获取保存对象实例的变量名:Javascript
【发布时间】:2015-01-08 22:19:09
【问题描述】:

我无法找到实现此目的的动态/通用方法。 我想要的是从对象函数中获取保存启动对象的变量的名称。

如果没有例子,这有点难以解释。 我不知道这样的程序是否有“技术/程序”名称。

举个例子

我启动一个对象为..

var jTest = new boom('hi');

我将对象构造函数作为..

function boom(message){
   console.log(message+' - '+objname);
   // what I want in the value of 'objname' is 'jTest'
   // I want to know dynamically what is the name of the variable that is going to hold this object instance (in this case 'jTest').
   // so that when I create html elements from this object
   // and they later have events I get a way to access the object instance
   // from inside the event functions.

   // for eg
   this.ref = document.createElement('div');
   this.ref.setAttribute('style','display:block;width:100px;height:100px;background:#f00');

   this.ref.setAttribute('data-obj',objname); // <- this obj name contains jTest or whatever name this object instance is stored as
   // then if i saved a name 'jTest' inside the element attr as a ref for later

   this.body.appendChild(this.ref);
   this.ref.addEventListener('click',(function(event){
       // this  - over here it refers to the element not the object instance jTest or any other forsaken name
       jTest.dance(); // now i want to run an object property called dance()
       // how to do this dynamically...?
       // you need to know the name of the variable that holds this object instance
       // so that you can call its property
       // this is where the attr "data-obj" value 'jTest' will help !
   }),true);
}

我想知道包含从对象内部对新创建的对象实例的引用的变量的字面名称(在本例中为“jTest”)。

这样我就可以为我从对象内部创建的 html 元素设置一个标识符、类 (.jTest)。

所以当我在这些 html 元素上有事件时 我会知道是哪个实例创建了它们,并且我可以访问相应的对象函数。

希望这是可以理解的。当我有新的想法来解释自己时,我会尝试更新描述。 还建议我解决此问题的其他方法'一种了解和调用从元素事件(悬停/单击)函数内部创建元素的对象实例的方法'

【问题讨论】:

  • jTest 不包含任何名称,但它包含指向新创建对象的地址
  • 正如我所说的......它很难解释和理解......我想知道保存的变量名称'jTest',它包含从对象内部对新创建的对象实例的引用实例。这样我就可以为我从对象内部创建的 html 元素设置一个标识符(.jTest)。所以当我在这些 html 元素上有事件时,我知道是哪个实例创建了它们,我可以访问对象函数。

标签: javascript oop object instance instance-variables


【解决方案1】:

我认为你应该看看这个问题:Determine name of a JavaScript object instance's class

但是,对象 jTest 不是“名称”,而是指向对象的指针。这里最大的问题应该是:为什么您想知道指向您正在使用的实例的指针的名称?您始终可以使用this 将您正在使用的实例传递给另一个对象。 (见:http://www.quirksmode.org/js/this.html

【讨论】:

  • 是的,这是正确的...“this”将指代对象本身,但是...我在事件“悬停、单击等...”函数中有它的用途。例如,在 html 元素的单击事件中...我需要对创建此元素的对象实例的引用。
  • @Vinodh:jTest 是对您的对象实例的引用。
  • @unikorn ya 我们知道,因为我将其写为“jTest”作为示例。当我将构造函数提供给其他人时,他将以不同的变量名启动它。我想知道那个变量名。
  • 我认为在这种情况下您需要详细说明您的问题。你想完成什么,你认为如何实现它?如果您想为某些对象添加事件,请不要将实例化类的名称添加到其中(例如 class='button1 jTest'),还有其他方法。
  • 例如:我的对象实例 'jTest' 创建了一些 html 元素。我在 jTest 中编写了事件函数来处理事情。但是现在我想要在这个被调用的事件函数中创建这个元素的对象的一些属性继续。在这种情况下,它的'jTest'。所以我可以在事件函数中调用 jTest.dance 并且它会起作用。但在一般情况下,我想动态地知道名称“jTest/whatever”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 2013-10-08
  • 2019-07-08
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 2014-11-05
相关资源
最近更新 更多