問題描述
我主要使用 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
注意:我在 a
和 b
的值動態(tài)變化的情況下工作
Note: I work in cases where the value of a
and b
changes dynamically
在我的工作中,我處理了很多來自 json、knockout 等的動態(tài)對象.所以 eval 解決了我的大部分問題.但是當我閱讀時,我發(fā)現(xiàn) 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.
任何人都可以建議一個插件或函數(shù)替代 eval() 記住我上面給出的例子
Can anyone suggest a plugin or function alternative to eval() keeping in mind the example i have given above
問題:
我正在使用剔除映射從 Json 數(shù)據(jù)創(chuàng)建一個表.這樣無論 json 的格式是什么,表格都會生成.我還使用敲除計算來計算一些字段.現(xiàn)在我使用硬編碼
self.Salary = ko.computed(function(){ return self.salaryEqn() && eval(self.salaryEqn()).toFixed(2); })
self.salaryEqn(salEqnTxt);
我想動態(tài)執(zhí)行這些方程.我可以將它動態(tài)地創(chuàng)建為字符串,但評估它們是我面臨的問題.
我想要解決方案
有沒有辦法在不使用 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 是一種非常靈活的語言.在非常非常少數(shù)情況下,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:
ie myobj.a
也可以被引用為 myobj['a']
.
ie myobj.a
could also be referenced as myobj['a']
.
由此,您可以使用變量作為下標,因此您可以動態(tài)引用 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()
.您可以根據(jù)需要構建 dynamicProperty
字符串,因此具有幾乎無限的靈活性.
No eval()
required. You can build the dynamicProperty
strings however you wish, so there's virtually infinite flexibility.
如果您的 a
和 b
變量是全局變量,則瀏覽器中的 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.a
或 window['a']
訪問,后者允許您可以執(zhí)行上述相同的 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!