The problem here is that the third line runs the constructor function with no arguments. In this case it’s fine, because it sets SubClass.prototype.thing to undefined. But what if the constructor has to do some kind of computation, or has required arguments, or (gasp) does I/O? Not the best coding practices in the world, sure, but I’ve worked with this kind of code.

In modern JS environments, the right thing to do is use Object.create:

SubClass.prototype=Object.create(BaseClass.prototype);

But if you want to support older browsers, a different strategy is called for.

It turns out, the Javascript runtime has no way of telling which constructor was used to create an object. Its instanceof operator only looks at the prototype chain. In environments supporting Object.getPrototypeOf we could implement instanceof ourselves:

functionisInstanceOf(obj,constructor){if(typeofconstructor!=='function')thrownewTypeError;if(typeofobj!=='object')thrownewTypeError;// look up the prototype chain for a matchwhile(true){if(obj===constructor.prototype)returntrue;if(obj===null)returnfalse;obj=Object.getPrototypeOf(obj);}}

So in particular, we can use a completely different constructor to make instances of the same function!