Skip to content

Commit 8e29d85

Browse files
committed
Prototypes
1 parent 735eb36 commit 8e29d85

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

OOP/prototype.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Prototypes are the mechanism by which JavaScript objects inherit features from one another.
2+
function Car(brand, year) {
3+
this.brand = brand;
4+
this.year = year;
5+
}
6+
Car.prototype.info = function () {
7+
return `${this.brand} ${this.year}`;
8+
};
9+
Car.prototype.speed = function (speed = "fast") {
10+
return `${this.info()} is ${speed}`;
11+
};
12+
let newCar = new Car("Toyota", 2020);
13+
let newCar2 = new Car("Honda", 2300);
14+
let newCar3 = new Car("BMW", 2100);
15+
16+
console.log(newCar2.info());
17+
console.log(newCar.speed());
18+
console.log(newCar3.speed("faster"));

0 commit comments

Comments
 (0)