Home > OS >  Can fn.constructor be different from Object.getPrototype(fn).constructor?
Can fn.constructor be different from Object.getPrototype(fn).constructor?

Time:09-26

I am writing a program that that serializes objects. As part of that, I need to get the class from an instance so I can create a new instance in the future. I noticed that I can either call fn.constructor or Object.getPrototypeOf(fn).constructor to get the class that the instance came from. For example:

class Foo {}
const myFoo = new Foo();

const constructor1 = myFoo.constructor;
const constructor2 = Object.getPrototypeOf(myFoo).constructor;

In the above, constructor1 and constructor2 seem to always be the same unless I manually set them to some other value. Is there a circumstance in which these values would be different?

CodePudding user response:

You have already identified the circumstance when they are different.

When you call Object.getPrototypeOf(myFoo).constructor, you are accessing the constructor property of the prototypical instance of Foo.

If you have an instance of Foo that doesn't directly have constructor defined, accessing it "passes through" myFoo and hits Foo.prototype. So, the only time the two would differ is if you manually redefined the constructor property on myFoo, like you said:

myFoo.constructor = NotFoo

In this case, JavaScript creates a property directly on the myFoo instance, so accessing myFoo.constructor never "passes through" to Foo.prototype.

The interesting case is when you are dealing with inheritance:

class Foo1 {}
class Foo2 extends Foo1{}

const myFoo2 = new Foo2()

const proto1 = Object.getPrototypeOf(myFoo2)
const proto2 = Object.getPrototypeOf(proto1)

proto1.constructor  // Foo2
proto2.constructor  // Foo1

CodePudding user response:

Is there a circumstance in which these values would be different?

Sure, and not just those where you did manually set the .constructor object on an instance.

There also exist "normal" objects with such a .constructor property, namely prototype objects like Foo.prototype.

Another case are objects without a prototype, like Object.create(null) or Object.prototype, where one of your expressions causes an exception but the other works.

Then there might be (construed) cases where .constructor is a getter that returns different values when invoked on an instance vs on the prototype object itself.

Finally, don't forget that constructor1 and constructor2 might be the same value, but not necessarily functions as you expect.

  • Related