title: Subclassing native objects: instanceof not working properly
body: <p>I'm trying to subclass the native JS <code>Error</code> object in CoffeeScript to get specialized error types, but i found that the <code>instanceof</code> does not work correctly if i don't define a constructor in the subclasses:</p>
  
  <pre><code>class SimpleError extends Error
  class EmptyConstructorError extends Error
    constructor: -&gt;
  class SuperConstructorError extends Error
    constructor: -&gt; 
      super
  
  new SimpleError instanceof SimpleError                     # -&gt; false
  new EmptyConstructorError instanceof EmptyConstructorError # -&gt; true
  new SuperConstructorError instanceof SuperConstructorError # -&gt; true
  </code></pre>
  
  <p>The problem seems to be caused by how the <a href="http://jashkenas.github.com/coffee-script/#try:class%20SimpleError%20extends%20Error%0Aclass%20EmptyConstructorError%20extends%20Error%0A%20%20constructor%3A%20-%3E%0Aclass%20SuperConstructorError%20extends%20Error%0A%20%20constructor%3A%20-%3E%20super%0A%0Aconsole.log%20new%20SimpleError%20instanceof%20SimpleError%0Aconsole.log%20new%20EmptyConstructorError%20instanceof%20EmptyConstructorError%0Aconsole.log%20new%20SuperConstructorError%20instanceof%20SuperConstructorError%0A" rel="nofollow noreferrer">generated JS</a> constructor functions are defined. When i <em>don't</em> define a constructor in CoffeeScript:</p>
  
  <pre><code>SimpleError = (function(_super) {
  
    __extends(SimpleError, _super);
  
    function SimpleError() {
      return SimpleError.__super__.constructor.apply(this, arguments);
    }
  
    return SimpleError;
  
  })(Error);
  </code></pre>
  
  <p>And when i <em>do</em> define a constructor in CoffeeScript:</p>
  
  <pre><code>SuperConstructorError = (function(_super) {
  
    __extends(SuperConstructorError, _super);
  
    function SuperConstructorError() {
      SuperConstructorError.__super__.constructor.apply(this, arguments);
    }
  
    return SuperConstructorError;
  
  })(Error);
  </code></pre>
  
  <p>As you can see, the difference is a simple <code>return</code> in the first case. I don't understand why this makes any difference in the <code>instanceof</code> behavior though, as the super constructor is just being applied to the <code>this</code> object (i.e. the super constructor is not being called with <code>new</code>), but then again i don't understand a whole lot of how JS constructors work =P</p>
  
  <p>And the weird thing is that this behavior seems to only happen when subclassing native JS objects. If i subclass CoffeeScript classes everything works as expected.</p>
  
  <p>Any idea of why this might be happening and how could i avoid writing dummy constructors just for the <code>instanceof</code> operator to work correctly?</p>
  
  <p>Thanks!</p>
  
  <h2>Update</h2>
  
  <p>So the user matyr <a href="https://stackoverflow.com/a/10807905/581845">answered</a> with a link to the commit where this behavior was introduced, but it doesn't quite explain what is happening here, so i'll try to explain that a little bit in case anyone else wonders why this works this way.</p>
  
  <p>The main problem is this inherited nasty "feature" from JavaScript which let us define a constructor function that returns an object other than the one being constructed:</p>
  
  <pre><code>function Foo() {
      return {'LOL': 'You fool!'};
  }
  new Foo() instanceof Foo // -&gt; false
  </code></pre>
  
  <p>And there is also the fact that some native constructors, like <code>Error</code>, <code>Array</code>, <code>String</code> and whatnot don't need to be called with <code>new</code>: they will just return a new object of the corresponding type if you happen to forget it.</p>
  
  <p>In the end, add these two ugly things together and the result is that you should remember to write <code>class MyError extends Error then constructor: -&gt; super</code> instead of the more intuitive <code>class MyError extends Error</code> if you want the <code>instanceof</code> operator to work properly with <code>MyError</code>. That's because CoffeeScript's implicit constructor will just return whatever the parent constructor returns, and in this case will do <code>return Error.apply(this, arguments)</code> which will just return a shinny new error object instead of the object you passed as the <code>this</code> argument. Yay!</p>
  
  <h2>Update 2 (Feb 25 2013)</h2>
  
  <p>This problem <a href="http://coffeescript.org/#changelog" rel="nofollow noreferrer">was fixed in CoffeeScript 1.5.0</a>! =D</p>
  
  <p>Now extending native objects works as expected:</p>
  
  <pre><code>class MyError extends Error
  new MyError instanceof MyError # -&gt; true :)
  </code></pre>
  
  <h2>Update 3 (Mar 04 2013)</h2>
  
  <p>Aaand it's gone on 1.6.0 =P </p>