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

209
Vistas
Why does a class have a "constructor" field in JavaScript?

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?

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