How to return null from a constructor called with new in Javascript?
<p>I'm trying to make the constructor abort the object construction if something fails, for example it can't get a hold of a canvas.  </p>

<p>But as I'm using <code>new</code> I see that klass() always returns <code>this</code> regardless of any return null or any other value, can I work around this to return null?</p>

<p>Now that I think of, a solution may be to create the new instance inside klass() and return that instance or null, and not use <code>new</code>, is there a better solution?</p>

<pre><code>function klass( canvas_id ) {
    var canvas = document.getElementById( canvas_id );

    if( ! ( canvas && canvas.getContext ) ) {
        return null;
    }   
}
var instance = new klass( 'wrong_id' );
console.log( instance, typeof instance );
</code></pre>
