I notice that when setting the prototype
to a new object, the way I define the constructor function make a difference:
function Bird(name) {
this.name = name;
}
// classic way, no problemo
Bird.prototype = {
numLegs: 2,
eat() {console.log("nom nom")}
};
but if I use the ES6 syntax:
class Bird {
constructor(name) {
this.name = name;
}
}
// throws error
Bird.prototype = {
numLegs: 2,
eat() {console.log("nom nom")}
};
the browser throws: TypeError: "prototype" is read-only
.
I’m still new to the class
and constructor
keywords. What’s happening here?
The ES6 syntax is still recommended right?
.
.
.
.
.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0
Challenge: Change the Prototype to a New Object
Link to the challenge: