我刚刚遇到了这个精美的javascript电子表格代码,以前从未见过:http : //jsfiddle.net/ondras/hYfN3/
它使用名为getter对象的单元格引用作为DATA对象的属性,并使用“ with”对单元格值进行范围评估。
//elm.id is the cell reference, DATA is an object whose properties are these getter wrappers
Object.defineProperty(DATA, elm.id, {get:getter});
Run Code Online (Sandbox Code Playgroud)
魔术发生在吸气剂中:
//My comments but jsfiddle code from Ond?ej Žára's fiddle
//Cell value getter function..
var getter = function() {
var value = localStorage[elm.id] || ""; //Direct cell contents
if (value.charAt(0) == "=") { //Got a formula, work it out
//strip the '=' and evaluate recursively with this getter func
with (DATA) return eval(value.substring(1));
} else { …Run Code Online (Sandbox Code Playgroud) javascript ×1