Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
Can fn.constructor be different from Object.getPrototype(fn).constructor?

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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
about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!