-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAboutObjects.js
107 lines (83 loc) · 3.01 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
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("Joker");
});
it("should confirm that properties are case sensitive", function() {
expect(meglomaniac.henchwoman).toBe( "Harley");
expect(meglomaniac.henchWoman).toBe(undefined);
});
});
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('They are Pinky and the Brain Brain Brain Brain').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(2019);
expect(meglomaniac.calculateAge()).toBe(49);
});
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(true);
});
it("should not have the detonator however", function() {
var hasDetonator = "theDetonator" in meglomaniac;
expect(hasDetonator).toBe(false);
});
});
it("should know that properties can be added and deleted", function() {
var meglomaniac = { mastermind : "Agent Smith", henchman: "Agent Smith" };
expect("secretary" in meglomaniac).toBe(false);
meglomaniac.secretary = "Agent Smith";
expect("secretary" in meglomaniac).toBe(true);
delete meglomaniac.henchman;
expect("henchman" in meglomaniac).toBe(false);
});
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(undefined);
expect(colouredCircle.colour).toBe('red');
Circle.prototype.describe = function() {
return "This circle has a radius of: " + this.radius;
};
expect(simpleCircle.describe()).toBe('This circle has a radius of: 10');
expect(colouredCircle.describe()).toBe('This circle has a radius of: 5');
});
});