Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jskoans.htm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<script type="text/javascript" src="topics/about_objects.js"></script>
<script type="text/javascript" src="topics/about_arrays.js"></script>
<script type="text/javascript" src="topics/about_reflection.js"></script>
<script type="text/javascript" src="topics/about_prototype_chain.js"></script>
<script type="text/javascript" src="topics/about_prototypal_inheritance.js"></script>
<script type="text/javascript" src="topics/about_functions_and_closure.js"></script>
<script type="text/javascript" src="topics/about_this.js"></script>
Expand Down
58 changes: 58 additions & 0 deletions topics/about_prototype_chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 childObj?", function () {
equals(child.a, __, 'what is \'a\' value?');
equals(child.b, __, 'what is \'b\' value?');
});

test("If 'b' was removed, whats b value?", function () {
delete child.b;
equals(child.b, __, 'what is \'b\' value now?');
});


// Is there a 'c' own property on childObj? No, check its prototype
// Is there a 'c' own property on childObj.[[Prototype]]? Yes, its value is...
test("Is there a 'c' own property on childObj.[[Prototype]]?", function () {
equals(child.hasOwnProperty('c'), __, 'childObj.hasOwnProperty(\'c\')?');
});

test("Is there a 'c' own property on childObj.[[Prototype]]?", function () {
equals(child.c, __, 'childObj.c?');
});


// Is there a 'd' own property on childObj? No, check its prototype
// Is there a 'd' own property on childObj.[[Prototype]]? No, check it prototype
// childObj.[[Prototype]].[[Prototype]] is null, stop searching, no property found, return...
test("Is there an 'd' own property on childObj?", function () {
equals(child.d, __, 'what is the value of childObj.d?');
});