-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideo08.js
40 lines (32 loc) · 841 Bytes
/
video08.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
// Inheritance: Complete Example
function Animal(name) {
this.name = name;
}
Animal.prototype.walk = function () {
console.log(this.name, 'is walking!');
}
var aDog = new Animal('myDog');
aDog.walk();
function Bird(name, wingsLength) {
// Call the Animal constructor
Animal.call(this, name);
this.wingsLength = wingsLength;
}
// Setup the prototype chain between Bird and Animal
// Bird.prototype.__proto__ = Animal.prototype;
Bird.prototype = Object.create(Animal.prototype, {
constructor: {
value: Bird,
enumerable: false,
writable: true,
configurable: true
}
});
Bird.prototype.fly = function () {
console.log(this.name, 'is flying!');
}
var aBird = new Bird('myBird', 35);
aBird.walk();
aBird.fly();
console.log(aBird.constructor.name);
console.log(aBird instanceof Bird, aBird instanceof Animal);