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

167
Vistas
Is a constructor ever defined on the object itself?

Is there ever a case where the constructor property would be defined on an object/function/class itself, or does it only ever appear on the prototype? For example:

let a = new Array();
console.log(a.hasOwnProperty('constructor'), a.constructor);
// false -- constructor not defined on Array, but Array.prototype

let f = function(){};
console.log(f.hasOwnProperty('constructor'), f.constructor);
// false -- constructor not defined on function, but function.prototype

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It would be technically possible for such a thing to occur, but it's a code smell.

function Klass(){
  this.constructor = Klass;
}
const k = new Klass();
console.log(k.hasOwnProperty('constructor'), k);

(You might also see it when someone improperly extends a function class)

Object instances are expected to have a .constructor property on their prototype, not directly on the instance. If property is directly on the instance, that'll cause problems when someone tries to use one of the common methods to iterate over own-properties and finds constructor there.

Another reason why it should be on the prototype and not on the object itself is to take advantage of prototypal inheritance. If a property can exist on one object, on the prototype, better to do that than to duplicate that property everywhere, for every instance. And the constructor property is already automatically on the prototype, so there shouldn't be any need to mess with it anyway.

For similar reasons, class methods are often on the class prototype when possible, rather than duplicated for every single instance. That is, you have all instances inheriting from one prototype with the method, rather than all instances having their own-property method. That is, it's not that good to have

class Klass {
  constructor() {
    this.method = () => console.log('method');
  }
}
const k = new Klass();
k.method();

with the method duplicated for every instance, when you can instead do

class Klass {
  method() {
    console.log('method');
  }
}
const k = new Klass();
k.method();

and have it only on the prototype.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Look at the doc of Object.prototype.hasOwnProperty.

Unlike the in operator, this method does not check for the specified property in the object's prototype chain.

The constructor property only exists in Object.prototype. And the prototype property only exists in the Object constructor.

enter image description here

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