Re:A spreadsheet in fewer than 30 lines of JavaScript, no li...
by angersock on Wednesday November 13, 2013 @11:27AM
(#6726263)
I'm a bit slow this morning...would anyone mind explaining how that and the defineProperties stuff makes a parse happen?
[ Parent
| on HN ]
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li...
by jcampbell1 on Wednesday November 13, 2013 @01:09PM
(#6727066)
50 getters are defined on DATA: a1..e5, and A1..E5
Consider the formula =A1+A2
inside the getter we have:
with(DATA) eval('A1+A2');
Which is basically the same as: eval('DATA.A1+DATA.A2');
Inside the eval, the getter for A1, and A2 will be called. This will continue until non-formulas are reached and value is returned. The other possibility, is you get an infinite amount of recursion, which causes an error that is caught in the try/catch block. For instance, if you put =A1 in A1, the DATA.A1 getter gets called 1000's of times, eventually causing a stack overflow, which is caught by the try/catch block.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li...
by jessedhillon on Wednesday November 13, 2013 @11:45AM
(#6726401)
defineProperty is being used to set a property On DATA, named corresponding to the cell's id (e.g., A3), to a descriptor. In this case the descriptor only provides a getter function, inside of which is an eval done with DATA as the context. That means all evaluated variable references will assume DATA as the implicit 'this' (whereas normally that would be 'window')