title: Javascript: How to get a list of an object's prototype chain constructor names?
body: <p>I recently encountered an interesting problem. I am developing with some existing code where, for better or worse, there are often several levels of prototypical inheritance ending in constructors for all kinds of various objects. Each of these ultimately inherit from a <em>single</em> base 'class'.</p>
  <p>Each constructor follows the following inheritance pattern, where <code>Sub</code> is a subclass and <code>Super</code> is it's immediate prototype:</p>
  <pre><code>function Sub(/*args*/) {
      Super.call(this,/*args*/);
  }
  
  Sub.prototype = Object.create(Super.prototype);
  </code></pre>
  <p>This pattern continues all the way up to the single base class.</p>
  <p>Discovering that it would be incredibly beneficial for our needs to store a list of constructor names for each object up the prototype chain, I came up with the following code addition for the base class, which uses the <code>callee</code> and <code>caller</code> properties of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments" rel="nofollow noreferrer"><strong>arguments</strong></a> object:</p>
  <pre><code>var curConstructor = arguments.callee;
  this.constructorList = [curConstructor.name];
  
  while(curConstructor.caller &amp;&amp; curConstructor.caller.name !== ''){
      curConstructor = curConstructor.caller;
      this.constructorList.push(curConstructor.name);
  }
  </code></pre>
  <p>The main issue with this, is it <em>only</em> works if the object was constructed inside of an anonymous function (or global scope). For instance, assuming I have a constructor <code>SubA1</code>with prototype <code>SubA</code>with prototype <code>Base</code>, if I do this:</p>
  <pre><code>function() {
    var aOk = new SubA1();
    function oops() {
      var aNotOk = new SubA1();
    }
    oops();
  }
  </code></pre>
  <p><code>aOk</code>'s constructor list is: <code>[Base,SubA,SubA1]</code>, which is what I want.</p>
  <p><code>aNotOk</code>'s constructor list ends up as: <code>[Base,SubA,SubA1,oops]</code>.</p>
  <p><a href="http://plnkr.co/edit/L74YDUt9jds7fn5PmkeS" rel="nofollow noreferrer"><strong>Here's a plunker</strong></a>.</p>
  <p>I could fix it by having each constructor add their name to the constructor list themselves, but there are several problems with that, not least of which is the tremendous pain (and debugging / maintenance nightmare) it would be to add code to each of the many many constructor functions.</p>
  <p>Is there an elegant way to achieve this?</p>
  <p>Note that although the above question was asked in a more general sense, and not specific to Node.js, I am actually working with code in a Node.js environment. Therefore, solutions specific to Node are welcome.</p>
  <h2>Update:</h2>
  <p>In my particular case, all of the constructors are within a namespace. So I can actually solve the problem by performing a check to make sure that the function name exists in the namespace before adding it to the <code>constructorList</code>.</p>
  <p>Basically, I can do this:</p>
  <pre><code>var curConstructor = arguments.callee;
  this.constructorList = [curConstructor.name];
  
  while(curConstructor.caller){
      curConstructor = curConstructor.caller;
  
      // Exit if the function name isn't part of the namespace
      if(typeof namespace[curConstructor.name] !== 'function') break;
  
      this.constructorList.push(curConstructor.name);
  }
  </code></pre>
  <p>This won't solve it in a generic sense though of course, and it isn't perfect. If the function which calls <code>new SubA1</code> for example just happens to have the same name as one of the constructors in the namespace, it will throw the list off. In my particular case, since the constructors follow a unique naming pattern, that occurrence is extremely unlikely.</p>
  <p>I am aware that <code>arguments.callee</code> is deprecated, and that <code>Object.constructor</code> is not trustworthy (it doesn't get me what I want anyway), but I am unaware of any other way to get names of each constructor in the chain by modifying the base 'class' only.</p>