|
| 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