-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (42 loc) · 1.21 KB
/
index.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
52
53
//First write out the super class (Person)
function Person(age, weight) {
this.age = age;
this.weight = weight;
}
//And give Person the ability to share their information
Person.prototype.getInfo = function () {
return (
"I am " + this.age + " years old " + "and weighs " + this.weight + " kilo."
);
};
//Next have a subclass of Person to Employee
function Employee(age, weight, salary) {
this.age = age;
this.weight = weight;
this.salary = salary;
}
Employee.prototype = new Person();
//Override the behavior of getInfo by defining one which is more fitting to an Employee
Employee.prototype.getInfo = function () {
return (
"I am " +
this.age +
" years old " +
"and weighs " +
this.weight +
" kilo " +
"and earns " +
this.salary +
" dollar."
);
};
//These can be used similar to your original code use
var person = new Person(50, 90);
var employee = new Employee(43, 80, 50000);
console.log(person.getInfo());
console.log(employee.getInfo());
/*However, there isn't much gained using inheritance
here as Employee's constructor is so similar to person's,
and the only function in the prototype is being overridden.
the power in polymorphic design is to share behaviors.
*/