Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从对象中获取保存对象实例的变量名:Javascript_Javascript_Oop_Object_Instance_Instance Variables - Fatal编程技术网

如何从对象中获取保存对象实例的变量名:Javascript

如何从对象中获取保存对象实例的变量名:Javascript,javascript,oop,object,instance,instance-variables,Javascript,Oop,Object,Instance,Instance Variables,我无法想出一个动态/通用的方法来实现这一点。 我想要的是从对象函数中获取保存启动对象的变量的名称 没有一个例子就很难解释这一点。 我不知道这种程序是否有“技术/程序”名称 举个例子 我启动一个对象作为 var jTest = new boom('hi'); 我将对象构造函数设置为 function boom(message){ console.log(message+' - '+objname); // what I want in the value of 'objname' i

我无法想出一个动态/通用的方法来实现这一点。 我想要的是从对象函数中获取保存启动对象的变量的名称

没有一个例子就很难解释这一点。 我不知道这种程序是否有“技术/程序”名称

举个例子

我启动一个对象作为

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);
}
功能动臂(信息){
console.log(消息+'-'+objname);
//我想要'objname'值中的'jTest'
//我想动态地知道保存这个对象实例的变量的名称(在本例中为“jTest”)。
//所以当我从这个对象创建html元素时
//然后他们会有一些事件,我有办法访问对象实例
//从事件函数内部。
//例如
this.ref=document.createElement('div');
this.ref.setAttribute('style','display:block;宽度:100px;高度:100px;背景:#f00');

this.ref.setAttribute('data-obj',objname);//我认为您应该看看这个问题:


但是,对象jTest不是一个“名称”,而是指向对象的指针。这里的大问题应该是:为什么您想知道指向您正在使用的实例的指针的名称?您可以始终使用
this
将您正在使用的实例传递给另一个对象(请参阅:)

jTest不包含任何名称,但它包含指向新创建对象的地址,正如我所说的……这很难解释和理解……我想知道保存的变量名“jTest”,它从对象实例内部保存对新创建对象实例的引用。这样我就可以设置标识符(.jTest)我从对象内部创建html元素。因此,当我在这些html元素上有事件时,我知道是哪个实例创建了它们,我可以访问对象函数。是的…“this”将指对象本身,但…我在事件“hover,click等”函数中有它的用法。在html元素的click事件中r示例…我需要一个对创建此元素的对象实例的引用。@Vinodh:jTest是对您的对象实例的引用。@unikorn ya我们知道这一点,因为我以“jTest”的形式编写了一个示例。.当我将构造函数交给其他人时,他将以不同的变量名启动它。我想知道该变量名。我想知道k在本例中,您需要详细说明一下您的问题。您想完成什么?您认为如何实现它?如果您想向某些对象添加事件,请不要向其中添加实例化类的名称(例如class='button1 jTest')例如,我的对象实例“jTest”创建了一些html元素。我在jTest中编写了事件函数来处理这些内容。但是现在我希望在这个被调用的事件函数中创建这个元素的对象的一些属性继续。在这个例子中是“jTest”。所以我可以在事件中调用jTest.dance但在一般情况下,我想动态地知道名称“jTest/whatever”。