-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter9_definitions_extends.js
51 lines (44 loc) · 1.2 KB
/
chapter9_definitions_extends.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
44
45
46
47
48
49
50
51
// var Person = function (name, age) {
// this.name = name;
// this.age = age;
// }
// Person.prototype.jump = function () {
// console.log('jump');
// }
// const person = new Person('bob', 20);
// person;
// person.jump();
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
jump() {
console.log('jump');
}
}
// Person.prototype.jump = function () {
// console.log('jump');
// }
const person = new Person('bob', 20);
person;
// person.jump();
class Employee extends Person {
constructor(name, age, years) {
super(name, age); // inherit Person's constructor
this.year = years;
}
quit() {
console.log('I quit my job');
this.year = 0;
}
jump() {
console.log('jump really high');
// this jump function will be overrided Person's jump function
}
}
const employee = new Employee('bob', 20, 10);
employee; // Employee { name: 'bob', age: 20, year: 10 }
employee.jump(); // jump really high
employee.quit(); // I quit my job
employee; // Employee {name: 'bob', age: 20, year: 0 }