Question

I've been going through the following article in Mozilla JS Guide.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Examples

In the example given for Classical inheritance with Object.create, i'm not able to figure out why the author added the following line (line# 21):

Rectangle.prototype.constructor = Rectangle;

Is it required for classical inheritance? Pls justify your answer.

Was it helpful?

Solution

As of ECMAScript-262 Edition 5.1, the constructor property is only set on function objects, see 13.2 Creating Function Objects (Step 17)

It is never read nor used by the language itself.

In particular,

obj instanceof C

does not use the constructor property, nor does

C.prototype.isPrototypeOf(obj)

So if you do not fashion it, and you do not use a framework which uses it, you may ignore the constructor property.

For further reading, see What’s up with the “constructor” property

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top