title: Using parasitic inheritance in Javascript, is it possible to implement introspective instance methods?
body: <p>I'm trying to develop a simple parasitic interface for my Javascript classes modeled after <a href="http://www.crockford.com/javascript/inheritance.html" rel="nofollow">Crockford</a> and I'd like to implement some basic introspective methods. Given the classes below, how can I write <code>class_name</code> and <code>is_a</code> methods that correctly identify the object class? </p>
  
  <pre><code>function Parent(name) {
      var self = {};
      self.name = name;
  
      self.init = function() {
          console.debug('init', self);
          return self;
      };
  
      self.ama = function() {
          return "AMA Parent named " + self.name;
      };
  
      // Introspective methods
      self.class_name = function() {
          // returns class name as a string
      };
  
      self.is_a = function(obj_class) {
          // returns boolean indicating whether object is instance of obj_class
      };
  
      self = self.init();
      return self;
  }
  
  function Child(name) {  
      var self = new Parent(name);
  
      var base_ama = self.ama;
      self.ama = function() {
          var ama = base_ama();
          return ama + ". NOT!";
      };
  
      self.init();
      return self;
  }
  </code></pre>
  
  <p>Here are the tests it would be expected to pass:</p>
  
  <pre><code>var alice = new Parent('Alice');
  assert_equal(alice.name, 'Alice');
  assert_equal(alice.class_name(), 'Parent');
  assert(alice.is_a(Parent));
  assert_not(alice.is_a(Child));
  assert_equal(alice.ama(), 'AMA Parent named Alice');
  
  var bob = new Child('Bob');
  assert_equal(bob.class_name(), 'Child');
  assert_not(bob.is_a(Parent));
  assert(bob.is_a(Child));
  assert_equal(bob.ama(), 'AMA Parent named Bob. NOT!');
  </code></pre>
  
  <p>You can find a fiddle here with tests set up:</p>
  
  <p><a href="http://jsfiddle.net/LTfVa/3/" rel="nofollow">http://jsfiddle.net/LTfVa/3/</a></p>