Last active
September 7, 2020 20:47
-
-
Save wentout/fc85ef2ea7925e2e4b7b6d817507000f to your computer and use it in GitHub Desktop.
Incremental Computation with Prototype Chain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // there is Object [null prototype] | |
| // under the hood, so we have room there | |
| var a = new Number(5); | |
| a.extract = function () { | |
| return a.valueOf(); | |
| }; | |
| var b = new Number(7); | |
| b.extract = function () { | |
| return b.valueOf(); | |
| }; | |
| console.log(Number.extract); | |
| Object.setPrototypeOf(b, a); | |
| console.log(b); | |
| console.log(b instanceof Number); | |
| console.log(b.valueOf()); | |
| console.log(b.extract()); | |
| console.log(Object.getPrototypeOf(b)); | |
| console.log(Object.getPrototypeOf(b).valueOf()); | |
| console.log(Object.getPrototypeOf(b).extract()); | |
| var s = {}; | |
| Object.setPrototypeOf(s, a); | |
| Object.setPrototypeOf(b, s); | |
| console.log(b instanceof Number); | |
| console.log(s instanceof Number); | |
| console.log(s instanceof Number); | |
| console.log(Object.getPrototypeOf(s).valueOf()); | |
| console.log(b.valueOf()); | |
| console.log(b.extract()); | |
| console.log(s.extract()); | |
| var m = { | |
| pick: 123 | |
| }; | |
| Object.setPrototypeOf(m, a); | |
| var n = new Number(9); | |
| Object.setPrototypeOf(n, m); | |
| console.log(n.valueOf()); | |
| console.log(Object.getPrototypeOf(n).pick); | |
| console.log(Object.getPrototypeOf(Object.getPrototypeOf(n)).extract()); | |
| console.log(m instanceof Number); | |
| console.log(m instanceof Object); | |
Author
wentout
commented
Sep 7, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment