|
| 1 | +function Person(name, age) { |
| 2 | + this.name = name; |
| 3 | + this.age = age; |
| 4 | +} |
| 5 | +Person.prototype.greet = function () { |
| 6 | + return `${this.name} is ${this.age} years old`; |
| 7 | +}; |
| 8 | +const rahim = new Person("Fahmid", 21); |
| 9 | +console.log(rahim.greet()); |
| 10 | + |
| 11 | +function Racer(name, age, car) { |
| 12 | + Person.call(this, name, age); |
| 13 | + this.car = car; |
| 14 | +} |
| 15 | + |
| 16 | +Racer.prototype = Object.create(Person.prototype); //get methods of Person {important} |
| 17 | +Racer.prototype.info = function () { |
| 18 | + return `${this.name} is a celebrity`; |
| 19 | +}; |
| 20 | + |
| 21 | +const racerOne = new Racer("Pain", 25, "BMW"); |
| 22 | +console.log(racerOne.age); |
| 23 | +console.log(racerOne.greet()); |
| 24 | + |
| 25 | +console.log(racerOne.info()); |
| 26 | + |
| 27 | +function Player(name, age, type) { |
| 28 | + Racer.call(this, name, age); |
| 29 | + this.type = type; |
| 30 | +} |
| 31 | +Player.prototype = Object.create(Racer.prototype); |
| 32 | +const labib = new Player("labib", 10, "Cricketer"); |
| 33 | +console.log(labib.type); |
| 34 | +console.log(labib.greet()); |
| 35 | + |
| 36 | +console.log(labib.constructor); // will indicate to Person |
| 37 | +Player.prototype.constructor = Player; // will indicate to Player {important} |
| 38 | +console.log(labib.constructor); |
0 commit comments