-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (36 loc) · 908 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
//Parent Class
const Car = function (make, speed) {
this.make = make;
this.speed = speed;
};
Car.prototype.accelerate = function () {
return (this.speed += 10);
};
Car.prototype.brake = function () {
return (this.speed -= 5);
};
//Child Class
const Ev = function (make, speed, charge) {
Car.call(this, make, speed);
this.charge = charge;
};
//Linking parent prototype
Ev.prototype = Object.create(Car.prototype);
Ev.prototype.constructor = Ev;
Ev.prototype.chargeBattery = function (chargeTo) {
return (this.charge = chargeTo);
};
Ev.prototype.accelerate = function () {
this.speed += 20;
this.charge -= 1;
console.log(
`Tesla going at ${this.speed}km/h, with a charge of ${this.charge}%`
);
};
const tesla = new Ev('Telsa', 120, 23);
tesla.accelerate();
console.log(tesla.brake());
console.log(tesla.chargeBattery(90));
tesla.accelerate();
console.log(tesla);