mardi 3 août 2021

How to modify method pattern to prototypal

Alright so how do I modify the following code from " Constructor pattern " to Prototypal pattern ? " Modify Human to use prototypal design pattern when it comes to it's methods."

 const getName = function () {
     return "name: " + this.name;
   };
 const getNameAndAge = function () {
     return ("name: " + this.name + "  " + "age: " + this.age);
  };
 const introduce = function () {
     return (" Hello I am " + this.name)
 };
 const Human = function (name, age) {

 this.name = name;
 this.age = age;

 this.getName=getName;
 this.getNameAndAge=getNameAndAge;
 this.introduce=introduce;
    
 };
// test cases
//const human2 = new Human("Jane", 25);
// human2.getName();
// human2.getNameAndAge();

I am a student and I am currently learning JS -> Patterns,ObjectOriented and Inheritance.

Please feel free to give any sort of advice even if its not related to the main issue.

Aucun commentaire:

Enregistrer un commentaire