javascript: new instance.constructor() equivalent to new Class()?

constructor, javascript, prototype

Solution

They are equivalent by default, but that isn't guaranteed to remain true.

When you define `Class`, `Class.prototype.constructor` is automatically defined. But if you were to write some code that changed the prototype:

Class.prototype = {};

Then Class.prototype.constructor would fall back to `Object.prototype.constructor`. Then it would correspond to `new Object()`, not `new Class()`.

To recap:

function Class() {}
var instance1 = new Class();

Class === instance1.constructor; // true

Class.prototype = {};
var instance2 = new Class();

instance1.constructor === instance2.constructor // false
Object === instance2.constructor // true

Problem

Given the following code ``` function Class(){} Class.prototype=... var instance1=new Class(); ``` are the following 2 lines equivalent? is there any inconvenient to line 1 (performance, compatibility...)? ``` var instance2=new instance1.constructor(); var instance2=new Class(); ``` Edit: I'm especially interested in the constructor method when using inheritance: to get the final Class constructor from the base Class (I can give an example if needed)

Original source