Is there a way to evaluate an equation such as "(1-2) / 4" without using eval() ?
Well, you can tokenize the expression and write your own evaluator that mimics what eval does. But although this may be useful in terms of limiting side effects (since eval is a very large hammer), it is extremely unlikely to work better than eval .
However, you can make the cache the result of evaluating all the other inputs so that you only evaluate the actual fuzzy input. It must be really effective.
For example, suppose you have this global object:
var values = { A7: /* initial value for A7 */, B7: /* initial value for B7 */, C7: /* initial value for C7 */, D7: /* initial value for D7 */, E7: /* initial value for E7 */, F7: /* initial value for F7 */, /* etc */ };
... and then bind this blur handler to all inputs:
$("input").blur(function() { values[this.id] = this.value;
... where doTheEvaluation used the values ββfrom values , rather than recounting all of them each time.
If this.value can reference other fields, you can do a recursive evaluation of this - but without evaluating all of your inputs.
source share