forked from liammclennan/JavaScript-Koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabout_prototype_chain.js
More file actions
63 lines (49 loc) · 1.9 KB
/
about_prototype_chain.js
File metadata and controls
63 lines (49 loc) · 1.9 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 objects prototype chain
// https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_and_the_prototype_chain
module("About Prototype Chain (topics/about_prototype_chain.js)");
var father = {
b: 3,
c: 4
};
var child = Object.create(father);
child.a = 1;
child.b = 2;
/*
* ---------------------- ---- ---- ----
* [a] [b] [c]
* ---------------------- ---- ---- ----
* [child] 1 2
* ---------------------- ---- ---- ----
* [father] 3 4
* ---------------------- ---- ---- ----
* [Object.prototype]
* ---------------------- ---- ---- ----
* [null]
* ---------------------- ---- ---- ----
* */
test("Is there an 'a' and 'b' own property on child?", function () {
equal(__, child.hasOwnProperty('a'), 'child.hasOwnProperty(\'a\')?');
equal(__, child.hasOwnProperty('b'), 'child.hasOwnProperty(\'b\')?');
});
test("Is there an 'a' and 'b' property on child?", function () {
equal(__, child.a, 'what is \'a\' value?');
equal(__, child.b, 'what is \'b\' value?');
});
test("If 'b' was removed, whats b value?", function () {
delete child.b;
equal(__, child.b, 'what is \'b\' value now?');
});
test("Is there a 'c' own property on child?", function () {
equal(__, child.hasOwnProperty('c'), 'child.hasOwnProperty(\'c\')?');
});
// Is there a 'c' own property on child? No, check its prototype
// Is there a 'c' own property on child.[[Prototype]]? Yes, its value is...
test("Is there a 'c' property on child?", function () {
equal(__, child.c, 'what is the value of child.c?');
});
// Is there a 'd' own property on child? No, check its prototype
// Is there a 'd' own property on child.[[Prototype]]? No, check it prototype
// child.[[Prototype]].[[Prototype]] is null, stop searching, no property found, return...
test("Is there an 'd' property on child?", function () {
equal(__, child.d, 'what is the value of child.d?');
});