forked from liammclennan/JavaScript-Koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabout_prototypal_inheritance.js
More file actions
61 lines (50 loc) · 1.95 KB
/
about_prototypal_inheritance.js
File metadata and controls
61 lines (50 loc) · 1.95 KB
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
55
56
57
58
59
60
61
// demonstrate the effect of modifying an objects prototype before and after the object is constructed
module("About Prototypal Inheritance (topics/about_prototypal_inheritance.js)");
// this 'class' pattern defines a class by its constructor
var Mammal = function(name) {
this.name = name;
}
// things that don't need to be set in the constructor should be added to the constructor's prototype property.
Mammal.prototype = {
sayHi: function() {
return "Hello, my name is " + this.name;
}
}
test("defining a 'class'", function() {
var eric = new Mammal("Eric");
equal(__, eric.sayHi(), 'what will Eric say?');
});
// add another function to the Mammal 'type' that uses the sayHi function
Mammal.prototype.favouriteSaying = function() {
return this.name + "'s favourite saying is " + this.sayHi();
}
test("more functions", function() {
var bobby = new Mammal("Bobby");
equal(__, bobby.favouriteSaying(), "what is Bobby's favourite saying?");
});
test("calling functions added to a prototype after an object was created", function() {
var paul = new Mammal("Paul");
Mammal.prototype.numberOfLettersInName = function() {
return this.name.length;
};
// the following statement asks the paul object to call a function that was added
// to the Mammal prototype after paul was constructed.
equal(__, paul.numberOfLettersInName(), "how long is Paul's name?");
});
// helper function for inheritance.
// From https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_Revisited
function extend(child, supertype){
child.prototype = supertype.prototype;
}
// "Subclass" Mammal
function Bat(name, wingspan) {
Mammal.call(this, name);
this.wingspan = wingspan;
}
// configure inheritance
extend(Bat, Mammal);
test("Inheritance", function() {
var lenny = new Bat("Lenny", "1.5m");
equal(__, lenny.sayHi(), "what does Lenny say?");
equal(__, lenny.wingspan, "what is Lenny's wingspan?");
});