-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.js
More file actions
41 lines (30 loc) · 1022 Bytes
/
inheritance.js
File metadata and controls
41 lines (30 loc) · 1022 Bytes
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
/*
Psuedo Classical Inheritance
Inheritance in Js is achieve through prototype chain.
*/
function Person (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.fullName = function() {
console.log(`${this.firstName} ${this.lastName}`);
}
// What does Object.create do?
// It just create an object with base as supplied object.
/*
var lee = { name: 'Lee chong wei', ranking: 1};
var lin = Object.create(lee);
var tty = Object.create(lee, {name: {value: 'TTY'}});
*/
function Professional (honorific, firstName, lastName) {
// Similar to calling super: Note this doesn't do inheritance.
Person.call(this, firstName, lastName);
this.honorific = honorific;
}
Professional.prototype = Object.create(Person.prototype);
Professional.prototype.profFullName = function () {
console.log(`Prof: ${honorific} ${this.firstName} ${this.lastName}`);
}
console.log(Person);
console.log(Professional.profFullName());
console.log(Professional.fullName());