Change the Prototype to a New Object - Constructor Syntax

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:

Class.prototype is not writable, hence, we can’t change it. Look into Object.defineProperty()

 function Bird(name) {
    this.name = name;
}

Object.defineProperty(Bird, "prototype", {
    writable: false,
});
Bird.prototype = {
    numLegs: 2,
    eat() {
        console.log("nom nom");
    },
};

console.log(Bird.prototype); // Bird constructor

We can’t no longer reassign Bird.prototype

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.