0

In JavaScript, how do you get the name of the variable that declared the new object.

  function asdf () {
       this.dosomething = functon () {
          var a = 1;
       };
  }

  var qwer = new asdf();

While within the scope of asdf(), I want to be able to find out the variable name that created this instance of the object.

seaBass
  • 585
  • 1
  • 6
  • 17
  • Variable names should be considered as meta-data and should not impact runtime behavior (unless of course you really are doing meta-programming). – Felix Kling Jun 21 '15 at 00:18

1 Answers1

0

You can't. Actually, the variable doesn't create the object at all. The variable just contains a reference to the object, but there could be multiple such references.

Also, there should be no need for it. Within asdf, you can use the this keyword to refer to the instance itself, which will be the same instance as qwer refers to.

If you need the instance inside the methods in asdf too, you can create a variable local to the object, like so:

 function asdf () {
   var memyself = this; // Store reference to `this`
   this.dosomething = functon () {
      var a = 1;
      // Use stored reference, because `this` will refer to `dosomething` here.
      mymyself.dosomethingelse();
   };

   this.dosomethingelse = functon () {
      alert('hello');
   };
 }

 var qwer = new asdf();

Another example, let the object bind itself to an event of an element. I deliberately put the names in the HTML already, but you could even generate all the HTML from an array of names. The HTML to start with should just contain an element in or after which to add the divs for each name.

Your object could be the one responsible for the life time of the div. If you create an object with a name as a parameter, it could create a div, add the text, attach the event handler, and even remove the div. The snippet below isn't that advanced, it just finds the element and attaches one of its methods to the click event of that element.

function asdf(element) {
  // If element is a string, use it as id to fetch an actual element. 
  // (Some error checking required in prodction code).
  if (typeof element === "string") {
    element = document.getElementById(element);
  }
  // Store references to myself and my element.
  var me = this;
  me.element = element; // 'this' could be used instead of 'me'.
  
  // Declare method
  this.doSomething = function() {
    alert(me.element.innerText); // Here, 'me' is required.
  }
  
  // Bind method to click event. 'this' could be used instead of 'me'.
  me.element.addEventListener('click', this.doSomething);
}

// Create three object for the three elements.

// You may store the object in a variable
a = new asdf('john');
// But you don't even need to. The object can just exist without external 
// reference (well, apart from the element which still uses its event handler).
new asdf('jane');
new asdf('nick');
Click a name to alert it.
<div class="button" id="john">John Doe</div>
<div class="button" id="jane">Jane Da</div>
<div class="button" id="nick">Nick Name</div>

I hope this is the problem that you are trying to solve and that this solves it. If you still need to get to quer, you're probably implementing a bad design. If you specify more details, I (or someone else) might find a better solution for you.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • The reason I'm trying to figure this out was so I could call onclick="qwer.dosomething()" in dynamically created html. What I'm doing now is just passing the variable qwer name as part of the arguments when I define it and that works fine, but I didn't know if there was a better way to do it. – seaBass Jun 21 '15 at 00:25
  • If you want to let the object assign itself to an element, you can do that without a variable. I've added a second snippet which shows you how it can be done. The only thing the object knows is the element. It can bind itself to the element. There is not even any need to store a reference in a variable as you can see. – GolezTrol Jun 21 '15 at 00:42
  • @Andrew: The solution here is not create HTML and use inline event handlers, but use the DOM API and closures (if necessary). Learn more about event handling from http://www.quirksmode.org/js/introevents.html. Next time I suggest to ask about the actual problem you are trying to solve. – Felix Kling Jun 21 '15 at 00:48