替代eval()javascript [英] Alternative to eval() javascript

查看:127
本文介绍了替代eval()javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要使用javascript,Jquery,knockout等

I work mainly with javascript, Jquery, knockout, etc

吸引eval()给我的是

The thing that attracted eval() to me is

var a = 5;
var b = 10;
eval("a+b");
//Gives me output 15

注意:我工作的情况是 a b 动态变化

Note: I work in cases where the value of a and b changes dynamically

在我的工作中我' m处理来自json,knockout等的大量动态对象。所以eval解决了我的大部分问题。
但是当我读到时,我发现eval()有很多问题,比如放慢速度等等。

In my work I'm dealing with a lot of dynamic objects from json, knockout, etc. So eval solves most of my problems. But as I read I found there are so many issues with eval() like slowing down etc.

我搜索了很多但没找到任何替代品对于eval(),当我必须评估方程式获取为方程式作为对象的方程式时。

I searched a lot and haven't found any substitute for eval() when i have to evaluate equation obtaining as string into equation as object.

任何人都可以建议使用插件或函数替代eval()时记住这个例子我已经给出了

Can anyone suggest a plugin or function alternative to eval() keeping in mind the example i have given above

问题:

我正在创建来自Json数据的表使用敲除映射。因此,生成json格式的表格。我还使用knockout计算了一些场。
现在我使用硬编码

self.Salary = ko.computed(function(){ return self.salaryEqn() && eval(self.salaryEqn()).toFixed(2); })
self.salaryEqn(salEqnTxt); 

我想动态执行这些方程式。我可以将它作为字符串动态地创建,但要评估它们是我面临的问题。

我想要解决方案

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

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

像公式一样

 "self.Salary = ko.computed(function(){ return self.salaryEqn() && eval(self.salaryEqn()).toFixed(2); })"


推荐答案

Javascript在这方面是一种非常灵活的语言。有很多非常的情况,其中 eval()是任何给定问题的正确答案,在这里肯定没有必要。

Javascript is a very flexible language in this regard. There are very very few cases where eval() is the right answer to any given question, and it certainly isn't necessary here.

如果您的 a b 变量是对象的一部分,您可以使用字符串下标访问它们:

If your a and b variables are part of an object, you can access them with string subscripts:

myobj.a 也可以引用为 myobj ['a']

从那里,您可以使用变量作为下标,因此您可以引用任何元素在 myobj 动态 - 即:

From that, you can use a variable for the subscript, and thus you can reference any element in myobj dynamically -- ie:

var myobj = {a : 5, b : 10};

var dynamicProperty1 = 'a';
var dynamicProperty2 = 'b';

//gives 15.
alert( myobj[dynamicProperty1] + myobj[dynamicProperty2] );

不需要 eval()。您可以根据需要构建 dynamicProperty 字符串,因此几乎具有无限的灵活性。

No eval() required. You can build the dynamicProperty strings however you wish, so there's virtually infinite flexibility.

如果你的 a b 变量是全局变量,浏览器中的JS全局变量实际上是窗口的子项 object,所以即使使用全局变量,你仍然可以使用这种技术。

If your a and b variables are globals, JS globals in the browser are actually children of the window object, so you can still use this technique even with globals.

即你的全局变量 a 可以也可以通过 window.a 窗口['a'] 访问,后一个选项允许你这样做上面描述的相同的 dynamicProperty 技巧。

ie your global variable a could also be accessed via window.a or window['a'], with the latter option allowing you to do the same dynamicProperty trick described above.

希望有所帮助。

这篇关于替代eval()javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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