I do not understand what is the interaction here - Object programmation Javascript

Hi everyone,

I was wondering what was the problem in my code.
I want my function Bird to inherit all the methods from my Animal constructor.
I would want to Bird to be able to access the Animal constructor & get the informations as this.*

EDIT : i used the prototype way to manage to get some answers while caling the Bird prototype, but still can’t access to the Animal informations … sadly
Your code so far

class Animal {
    constructor(name, age, color, personnality) {
        this.name = name;
        this.age = age;
        this.color = color;
        this.personnality = personnality;
        this.presentation = function() {
            console.log(`Cela peut paraitre étonannt oui, mais mon prénom est ${this.name}, j'ai ${this.age} mais attention, je suis ${this.color}`);
          };
    }
}

Bird.prototype = Object.create(Animal.prototype);
//Bird.prototype = new Animal;

function Bird() { } 
Bird.prototype = {  
    constructor: Bird,
    eat: function() {
    console.log("i am eating well, thanks");
  }
};

let bolo = new Bird('Roger', 5, 'rouge'); 
//For this element, i am switching with Bird & Animal, while i type Bird, it can't access to the presentation function.
bolo.eat(); //Uncaught TypeError TypeError: bolo.presentation is not a function

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Object Oriented Programming - Use Inheritance So You Don’t Repeat Yourself

Link to the challenge:

Answer :

class Onimal {
constructor(name, age, color, personnality) {
this.name = name;
this.age = age;
this.color = color;
this.personnality = personnality;
}
eat() {
alert(It may seem strange, but my first name is ${this.name}, I have ${this.age} but beware, i might be toxic ! I am ${this.color});
}
}

class Bird extends Onimal {
constructor(name, age, color){
// call the Onimal constrcutor.
// But personnality value will be undefiend here. You can take that as an argument to Bird constructor as well.
super(name, age, color);
}
}

let yolo = new Bird(‘Roger’, 5, ‘red’);
yolo.eat()

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