Well I'm a javascript noob myself but I'll try to explain. The clever thing is how he used eval.
This is where the magic happens :
var getter = function() {
var value = localStorage[elm.id] || "";
if (value.charAt(0) == "=") {
with (DATA) return eval(value.substring(1));
} else { return isNaN(parseFloat(value)) ? value :
parseFloat(value); }
};
So if you entered "=A1 + A2" it would be evaluated as
DATA[A1]+DATA[A2] . A1 and A2 get bound to DATA because of the preceding with statement.
Now the question is what happens when DATA[A1] is called.
He used Object.defineProperty to ensure that whenever you try to access a cell the same method (which i just explained) gets called.
This means add a property called elm.id to DATA and it's getter is the function I just explained.
So whenever we try to acess DATA[something] it hooks into the same getter method so a cell can depend on another cell which depends on another cell and so on ..
Now you can see why this so clever. Because he used the inbuilt eval it can evaluate any kind of formula involving multiplication , division or function calls.
The only thing I dont understand is how circular references are prevented.
Like I said I'm not very familiar with JS so if anyone notices anything wrong I'd like to know.
Circular references are not really "prevented". Most browsers just stackoverflow after a couple of thousand calls. The code below try/catches that error and simply continues.