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

100
Visualizações
Javascript Inheritance Chain
function Student() {
}

Student.prototype.sayName = function() {
  console.log(this.name)
}

function EighthGrader(name) {
  this.name = name
  this.grade = 8
}

EighthGrader.prototype = Object.create(Student)

const carl = new EighthGrader("carl")
carl.sayName() // console.logs "carl" TypeError HERE
carl.grade // 8

Why do I get a TypeError when I use Student as the argument for Object.create? I know it can be fixed using Student.prototype instead.

I thought the sayName() method could still be accessed according to the prototype chain?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are getting this error because sayName is not defined on Student and therefore not on EighthGrader.prototype. sayName is defined on Student.prototype, so the following is the correct way of establishing inheritance:

EighthGrader.prototype = Object.create(Student.prototype);

Inheritance always works by having the child class' prototype "inherit" from the parent class' prototype.

See also Benefits of using `Object.create` for inheritance

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