forked from mrdavidlaing/javascript-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAboutObjects.js
109 lines (83 loc) · 2.99 KB
/
AboutObjects.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
describe("About Objects", function () {
describe("Properties", function () {
var meglomaniac;
beforeEach(function () {
meglomaniac = { mastermind: "Joker", henchwoman: "Harley" };
});
it("should confirm objects are collections of properties", function () {
expect(meglomaniac.mastermind).toBe(FILL_ME_IN);
});
it("should confirm that properties are case sensitive", function () {
expect(meglomaniac.henchwoman).toBe(FILL_ME_IN);
expect(meglomaniac.henchWoman).toBe(FILL_ME_IN);
});
});
it("should know properties that are functions act like methods", function () {
var meglomaniac = {
mastermind : "Brain",
henchman: "Pinky",
battleCry: function (noOfBrains) {
return "They are " + this.henchman + " and the" +
Array(noOfBrains + 1).join(" " + this.mastermind);
}
};
var battleCry = meglomaniac.battleCry(4);
expect(FILL_ME_IN).toMatch(battleCry);
});
it("should confirm that when a function is attached to an object, 'this' refers to the object", function () {
var currentDate = new Date()
var currentYear = (currentDate.getFullYear());
var meglomaniac = {
mastermind: "James Wood",
henchman: "Adam West",
birthYear: 1970,
calculateAge: function () {
return currentYear - this.birthYear;
}
};
expect(currentYear).toBe(FILL_ME_IN);
expect(meglomaniac.calculateAge()).toBe(FILL_ME_IN);
});
describe("'in' keyword", function () {
var meglomaniac;
beforeEach(function () {
meglomaniac = {
mastermind: "The Monarch",
henchwoman: "Dr Girlfriend",
theBomb: true
};
});
it("should have the bomb", function () {
var hasBomb = "theBomb" in meglomaniac;
expect(hasBomb).toBe(FILL_ME_IN);
});
it("should not have the detonator however", function () {
var hasDetonator = "theDetonator" in meglomaniac;
expect(hasDetonator).toBe(FILL_ME_IN);
});
});
it("should know that properties can be added and deleted", function () {
var meglomaniac = { mastermind : "Agent Smith", henchman: "Agent Smith" };
expect("secretary" in meglomaniac).toBe(FILL_ME_IN);
meglomaniac.secretary = "Agent Smith";
expect("secretary" in meglomaniac).toBe(FILL_ME_IN);
delete meglomaniac.henchman;
expect("henchman" in meglomaniac).toBe(FILL_ME_IN);
});
it("should use prototype to add to all objects", function () {
function Circle(radius)
{
this.radius = radius;
}
var simpleCircle = new Circle(10);
var colouredCircle = new Circle(5);
colouredCircle.colour = "red";
expect(simpleCircle.colour).toBe(FILL_ME_IN);
expect(colouredCircle.colour).toBe(FILL_ME_IN);
Circle.prototype.describe = function () {
return "This circle has a radius of: " + this.radius;
};
expect(simpleCircle.describe()).toBe(FILL_ME_IN);
expect(colouredCircle.describe()).toBe(FILL_ME_IN);
});
});