Skip to content

Commit ed1cbb6

Browse files
committed
added about_prototype_chain
1 parent 5e46b41 commit ed1cbb6

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

jskoans.htm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<script type="text/javascript" src="topics/about_objects.js"></script>
1818
<script type="text/javascript" src="topics/about_arrays.js"></script>
1919
<script type="text/javascript" src="topics/about_reflection.js"></script>
20+
<script type="text/javascript" src="topics/about_prototype_chain.js"></script>
2021
<script type="text/javascript" src="topics/about_prototypal_inheritance.js"></script>
2122
<script type="text/javascript" src="topics/about_functions_and_closure.js"></script>
2223
<script type="text/javascript" src="topics/about_this.js"></script>

topics/about_prototype_chain.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// demonstrate objects prototype chain
2+
3+
// https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_and_the_prototype_chain
4+
module("About Prototype Chain (topics/about_prototype_chain.js)");
5+
6+
var father = {
7+
b: 3,
8+
c: 4
9+
};
10+
11+
var child = Object.create(father);
12+
child.a = 1;
13+
child.b = 2;
14+
15+
/*
16+
* ---------------------- ---- ---- ----
17+
* [a] [b] [c]
18+
* ---------------------- ---- ---- ----
19+
* [child] 1 2
20+
* ---------------------- ---- ---- ----
21+
* [father] 3 4
22+
* ---------------------- ---- ---- ----
23+
* [Object.prototype]
24+
* ---------------------- ---- ---- ----
25+
* [null]
26+
* ---------------------- ---- ---- ----
27+
* */
28+
29+
test("Is there an 'a' and 'b' own property on childObj?", function () {
30+
equals(child.a, __, 'what is \'a\' value?');
31+
equals(child.b, __, 'what is \'b\' value?');
32+
});
33+
34+
test("If 'b' was removed, whats b value?", function () {
35+
delete child.b;
36+
equals(child.b, __, 'what is \'b\' value now?');
37+
});
38+
39+
40+
// Is there a 'c' own property on childObj? No, check its prototype
41+
// Is there a 'c' own property on childObj.[[Prototype]]? Yes, its value is...
42+
test("Is there a 'c' own property on childObj.[[Prototype]]?", function () {
43+
equals(child.hasOwnProperty('c'), __, 'childObj.hasOwnProperty(\'c\')?');
44+
});
45+
46+
test("Is there a 'c' own property on childObj.[[Prototype]]?", function () {
47+
equals(child.c, __, 'childObj.c?');
48+
});
49+
50+
51+
// Is there a 'd' own property on childObj? No, check its prototype
52+
// Is there a 'd' own property on childObj.[[Prototype]]? No, check it prototype
53+
// childObj.[[Prototype]].[[Prototype]] is null, stop searching, no property found, return...
54+
test("Is there an 'd' own property on childObj?", function () {
55+
equals(child.d, __, 'what is the value of childObj.d?');
56+
});
57+
58+

0 commit comments

Comments
 (0)