如果我运行代码:
obj = {};
obj['number'] = 1;
obj['expressionS'] = 'Sin(0.5 * c1)';
obj['compiledExpressionS'] = null;
let cnr = 'c' + obj.number;
let params = {};
params[cnr] = 2;
var parsedExpressionS = math.parse(obj.expressionS);
obj.compiledExpressionS = parsedExpressionS.compile();
let value = obj.compiledExpressionS.eval(params);
我将得到一个错误(在最后一行中抛出): (中间值)(中间值)(中间值)不是函数。这里使用Mathjs库(文档:http://mathjs.org/docs/expressions/parsing.html)。我不知道为什么会出现错误。
答案 0 :(得分:1)
问题在于您尚未遵守该值,
obj.compiledExpressionS = parsedExpressionS.compile(math);
Sin
应该是小写
正在工作的小提琴:http://jsfiddle.net/oxb3sp5o/414/
工作代码
obj = {};
obj['number'] = 1;
obj['expressionS'] = 'sin(0.5 * c1)';
obj['compiledExpressionS'] = null;
let cnr = 'c' + obj.number;
let params = {};
params[cnr] = 2;
var parsedExpressionS = math.parse(obj.expressionS);
obj.compiledExpressionS = parsedExpressionS.compile(math);
let value = obj.compiledExpressionS.eval(params);
console.log(value);