Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda