1

I am not able to figure out a dynamic/general way to achieve this. What I want is to get the name of the variable which holds the initiated object from within the object functions.

Its kind of hard to explain this without an example. I dont know if there is a "technical/programmatic" name for such a procedure.

Take an example

I initiate an object as..

var jTest = new boom('hi');

I have the objects constructor as..

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);
}

I want to know the literal name of the variable(in this case 'jTest') which contains the reference to the newly created object instance from inside of the object.

So that I can set an identifier, class (.jTest) to the html elements I create from inside of the object.

So when I have events on those html elements I will know which instance created them and I can access the appropriate object functions.

Hope this is understandable. I will try to update the description as i get new ideas to explain myself. Also suggest me other ways to solve this problem 'A way to know and call an object instance that created an element from inside the elements event(hover / click) function'.

Vinodh
  • 41
  • 5
  • 3
    jTest doesnt hold any name but it holds the address that points to the newly created object – Prabhu Murthy Nov 12 '14 at 06:28
  • as i said... its difficult to explain and to understand... i want to know the saved variable name 'jTest' which holds the reference to the newly created object instance from inside of the object instance. so that i can set an identifier (.jTest) to the html elements i create from inside of the object. so when i have events on those html elements i know which instance created them and i can access the object functions. – Vinodh Nov 12 '14 at 06:58

1 Answers1

2

I think you should be looking at this question : Determine name of a JavaScript object instance's class

But, the object jTest is not a "name", but pointer to the object. The big question here should be: why would you want to know the name of the pointer to the instance you're working with? You could always pass the instance you're working with to another object with this. (see: http://www.quirksmode.org/js/this.html )

Community
  • 1
  • 1
Leon
  • 919
  • 6
  • 21
  • ya that is correct... 'this' will refer to the object itself but ... I have its uses within event 'hover,click etc...' functions. Inside a click event of an html element for example... I need a reference to the object instance that created this element. – Vinodh Nov 12 '14 at 06:50
  • @Vinodh: jTest is the reference to your object instance. – Prabhu Murthy Nov 12 '14 at 06:54
  • @unikorn ya we know that because i have written it as 'jTest' for an example.. when i give my constructor to someone else he will initiate it in a different variable name. I want to know that variable name. – Vinodh Nov 12 '14 at 07:15
  • I think you need to elaborate your question a bit in this case. What is it you want to accomplish and how did you think to implement it? If you want to add an event to certain objects, don't add the name of the instanciating class to it (e.G. like class='button1 jTest'), there are other methods for that. – Leon Nov 12 '14 at 07:20
  • for eg: my object instance 'jTest' creates some html elements. I have event functions written inside jTest to handle stuff. But now I want some properties of the object that created this element inside this called event function to continue. In this case its 'jTest'. So i can call jTest.dance inside the event function and it will work. but in a general case i want to know the name 'jTest / whatever' dynamically. – Vinodh Nov 12 '14 at 07:28
  • i've edited the question... hope its a little bit clear now.. I'll try to improve the question as i get ways to express what i want. – Vinodh Nov 12 '14 at 07:46
  • @Leon your answer contains a link where "Drew Noakes" wants to know the constructors name from inside of the instantiated object. I want to know the name of the variable that holds the object instance from within the object instance. – Vinodh Nov 12 '14 at 08:11