在javascript中计算字符串值,而不是使用eval [英] Calculate string value in javascript, not using eval

查看:123
本文介绍了在javascript中计算字符串值,而不是使用eval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不使用 eval 的情况下计算存储在字符串中的公式?

Is there a way to calculate a formula stored in a string in JavaScript without using eval?

通常情况下我会做类似的事情

Normally I would do something like

var apa = "12/5*9+9.4*2";
alert(eval(apa));

那么,有没有人知道 eval的替代品

推荐答案

这正是你应该使用eval的地方,或者你必须循环遍历字符串并生成数字。您将不得不使用isNaN方法来执行此操作。

This exactly the place where you should be using eval, or you will have to loop through the string and generate the numbers. You will have to use isNaN method to do it.

这篇关于在javascript中计算字符串值,而不是使用eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆