A class is supposed to have a prototype field that supplies default fields. For example, prototype.constructor defaults to the constructor function (i.e., the class itself) and is therefore accessible as the constructor field in new objects:
function MyClass(){}
console.log(MyClass.prototype.constructor === MyClass); // true
console.log(new MyClass().constructor === MyClass); // true
That's all fine and good. However, if we accidently confuse the class and the object, we would expect an error, since the class shouldn't have a constructor field. But it does:
console.log("constructor" in MyClass); // true
console.log(MyClass.constructor === MyClass); // false
Why does the class itself have a constructor field, and why is it not the same as protoype.constructor? Wouldn't this lead to lots of bugs, since a program could inadvertently access the constructor field of a class?
MyClassit itself an object: It's aFunctionobject.MyClass.constructoris therefore the Function constructor!console.log(MyClass.constructor == Function.prototype.constructor) /* true */var newInstance = new myObject.constructor();. Obviously you could useObject.createtoo, but whateverr. Not sure there's much risk of programs "accidentally" accessing aconstructorproperty any more than "accidentally" accessing its__proto__property...object.constructoris the constructor for creating another instance of that object. Classes are objects too. They are Function objects. SoMyClass.constructoris the constructor for creating another Function object.MyClass.constructor("blah")is just a confusing way of writingnew Function("blah").