Skip to content

Instantly share code, notes, and snippets.

@wentout
Last active October 16, 2020 14:12
Show Gist options
  • Select an option

  • Save wentout/811d24b3b3be454d32a1e926a674a625 to your computer and use it in GitHub Desktop.

Select an option

Save wentout/811d24b3b3be454d32a1e926a674a625 to your computer and use it in GitHub Desktop.
Example of Incremental Computation from Number to Array to Object
'use strict';
const ogp = Object.getPrototypeOf;
const myNumber = new Number(5);
const arr = new Array(1, 2, 3);
const example = {};
console.log(ogp(arr)); // Object(0) []
console.log(ogp(ogp(arr))); // [Object: null prototype] {}
Object.setPrototypeOf(example, arr);
Object.defineProperty(myNumber, 'extracted', {
get () {
return myNumber.valueOf();
}
});
Object.defineProperty(myNumber, Symbol.toPrimitive, {
get () {
return function () {
return myNumber.valueOf();
};
}
});
Object.defineProperty(example, 'addition', {
get () {
return example.map(val => val + myNumber);
}
});
Object.setPrototypeOf(ogp(arr), myNumber);
console.log('example instanceof Array → ', example instanceof Array); // true
console.log('example instanceof Object → ', example instanceof Object); // true
console.log('example instanceof Number → ', example instanceof Number); // true
console.log('example[0] → ', example[0]); // 1
console.log('example.extracted → ', example.extracted); // 5
console.log('example + 2 → ', example + 2); // 7
console.log('example.addition → ', example.addition); // [6, 7, 8]
debugger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment