Skip to content

Commit a889765

Browse files
committed
Method Overriding
1 parent 46ba2b7 commit a889765

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

OOP/method-overriding.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
9+
function Racer(name, age, car) {
10+
Person.call(this, name, age);
11+
this.car = car;
12+
}
13+
14+
Racer.prototype = Object.create(Person.prototype);
15+
Racer.prototype.greet = function () {
16+
return `${Person.prototype.greet.call(this)} . He is a celebrity`;
17+
//method overriding
18+
};
19+
20+
const racerOne = new Racer("Pain", 25, "BMW");
21+
console.log(racerOne.age);
22+
console.log(racerOne.greet());

0 commit comments

Comments
 (0)