Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda