-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototypal.js
54 lines (39 loc) · 1.43 KB
/
prototypal.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
54
(function () {
"use strict";
var Snake = {};
Snake.sound = 'Hiss';
Snake.speak = function () {
return this.sound + '!!';
};
var Constrictor = Object.create(Snake);
Constrictor.constrict = function () {
return 'The ' + this.name.toLowerCase() +' squeezes you to death!';
};
var Venomous = Object.create(Snake);
Venomous.invenomate = function () {
return 'The ' + this.name.toLowerCase() +' bites you and the poison kills you!';
};
var python = Object.create(Constrictor);
python.name = 'Python';
var cobra = Object.create(Venomous);
cobra.name = 'Cobra';
console.log('From the base class both can \'speak\' using a shared method and shared instance variable');
console.log(python.name + ':\n' + python.speak());
console.log(cobra.name + ':\n' + cobra.speak());
console.log('They have access to their sub-class methods: \n');
console.log(python.name + ':\n' + python.constrict());
console.log(cobra.name + ':\n' + cobra.invenomate());
console.log('...but not to each others: \n');
try {
console.log(python.name + ' invenomating: \n');
python.invenomate();
} catch (e) {
console.log('A python cannot invenomate\n', e);
}
try {
console.log(cobra.name + ' constricting: \n');
cobra.constrict();
} catch (err) {
console.log('A cobra cannot constrict\n', err);
}
}());