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

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

问题描述

我主要使用 javascript、Jquery、knout 等工作

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

注意:我在 ab 的值动态变化的情况下工作

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

在我的工作中,我处理了很多来自 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 的格式是什么,表格都会生成.我还使用敲除计算来计算一些字段.现在我使用硬编码

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.

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

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

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

ie myobj.a could also be referenced as 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.

如果您的 ab 变量是全局变量,则浏览器中的 JS 全局变量实际上是 window 对象的子对象,因此您仍然可以即使使用全局变量也可以使用这种技术.

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.awindow['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天全站免登陆