Last active
January 6, 2018 13:33
-
-
Save chrispsn/f4e37338a929f9bb43b7d0e9f4117ee7 to your computer and use it in GitHub Desktop.
Objects with self-referencing getters can be used like functions in JavaScript. Because Mesh spreadsheets are just objects, we can use this to turn spreadsheets into functions.
This file contains 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
// Define a sheet (ATTACHMENTS omitted for brevity) | |
const calc = { | |
term: 3, | |
rate: 0.05, | |
start_value: 100, | |
get end_value() {return this.start_value * (1 + this.rate) ** this.term} | |
} | |
// Simple example of using a sheet as a template - | |
// equivalent in Excel would be overwriting a cell, | |
// but this lets you do it programmatically within the sheet UI | |
const new_calc = {__proto__: calc, start_value: 200} | |
console.log("Simple example:", new_calc.end_value) | |
// Scenario analysis across arbitrary inputs - would require a data table in Excel | |
const scenarios = { | |
terms: [1, 2, 3, 4, 5], | |
get end_values() {return this.terms.map(t => ({__proto__: calc, term: t}.end_value))} | |
} | |
console.log("Scenarios 1:", scenarios.end_values) | |
// Can also define the scenario analysis *within* the same sheet; | |
// need to be careful with use of 'this', though, if the objects get more nested | |
const calc_2 = { | |
term: 3, | |
rate: 0.05, | |
start_value: 100, | |
get end_value() {return this.start_value * (1 + this.rate) ** this.term}, | |
get scenarios() {return [1, 2, 3].map(t => ({__proto__: this, term: t}.end_value))} | |
// could also use calc_2 instead of this: | |
// get scenarios() {return [1, 2, 3].map(t => ({__proto__: calc_2, term: t}.end_value))} | |
} | |
console.log("Scenarios 2:", calc_2.scenarios) | |
// Finally, we can omit 'this' from the getter variable lookups using 'with', | |
// at the expense of 'use strict' compliance. | |
const calc_3 = { | |
term: 3, | |
rate: 0.05, | |
start_value: 100, | |
get end_value() {with (this) {return start_value * (1 + rate) ** term}}, | |
get scenarios() {return [1, 2, 3].map(t => ({__proto__: this, term: t}.end_value))} | |
} | |
console.log("Scenarios 3:", calc_3.scenarios) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment