Difference between setting the prototype of an object using Object.create() vs assigning the prototype of one object to the prototype of another

59 views Asked by At
function Student() {
}

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

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


EighthGrader.prototype = Student.prototype

function NinthGrader(name) {
  this.name = name
  this.grade = 9
}


NinthGrader.prototype = Student.prototype

NinthGrader.prototype.sayName = function() {console.log("HAHAHAHAHAHA")}

const carl = new EighthGrader("carl")
carl.sayName() 

I tried to read the MDN Documentation of Object.create() and i wasn't able to figure out why does console.log(carl.sayName());print "HAHAHAHAHAHA"and not "carl" when i added a method to the prototype object of the NinthGrader object.

0

There are 0 answers