Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!