在JS中使用字符串值作为计算

时间:2015-07-10 08:30:10

标签: javascript

我在DB中存储一个值(称为generalparameter)。

这个值是我在JS代码中调用的计算。

问题是值是一个字符串,但我希望它是一个计算值。

e.g。 我的DB值是: -

(((this.amount / new_rate) - this.stock_payment).toFixed(2) / (this.amount / new_rate) * 100).toFixed(2)

我的代码是: -

application.getGeneralParameter('UI','CAL_NEW_MARGIN');

但这会产生: -

"(((this.amount / new_rate) - this.stock_payment).toFixed(2) / (this.amount / new_rate) * 100).toFixed(2)"

如何摆脱“”?

感谢。

伊恩。

2 个答案:

答案 0 :(得分:2)

这没有实际的eval关键字 - 但是函数也会评估,因此如果可以在DB中保存任意字符串,建议谨慎,因为它将在您所在的网站范围内执行。



blockquote:before, q:before {
  font-size: 6rem;
  line-height: 0.9em;
}




答案 1 :(得分:0)

尝试使用eval()功能,如下所示:

eval("(((this.amount / new_rate) - this.stock_payment).toFixed(2) / (this.amount / new_rate) * 100).toFixed(2)");

或者像这样:

eval(application.getGeneralParameter('UI','CAL_NEW_MARGIN'));