Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to eval() javascript [duplicate]

I work mainly with javascript, Jquery, knockout, etc

The thing that attracted eval() to me is

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

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

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.

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.

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

Problem:

I'm creating a Table from Json data using knockout mapping. So that what ever the format of json is the table is generated. I also calculate some field using knockout computed. Right now I use hard-coded

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

I want to execute these equations dynamic. I can create it dynamicaly as string but to eval them is the issue I'm facing.

I want solution for

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

Like a formula

 "self.Salary = ko.computed(function(){ return self.salaryEqn() && eval(self.salaryEqn()).toFixed(2); })" 
like image 673
Okky Avatar asked Apr 16 '13 12:04

Okky


People also ask

What is replacement of eval in JavaScript?

An alternative to eval is Function() . Just like eval() , Function() takes some expression as a string for execution, except, rather than outputting the result directly, it returns an anonymous function to you that you can call. `Function() is a faster and more secure alternative to eval().

Why we should not use eval in JavaScript?

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension.

Is eval function bad?

It is a possible security risk, it has a different scope of execution, and is quite inefficient, as it creates an entirely new scripting environment for the execution of the code. See here for some more info: eval.

Is eval deprecated?

The eval server command has been deprecated since MongoDB 3.0 and is definitely not recommendable for performance or security reasons.


2 Answers

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.

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

ie myobj.a could also be referenced as myobj['a'].

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] ); 

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

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.

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.

Hope that helps.

like image 82
Spudley Avatar answered Oct 10 '22 23:10

Spudley


do you mean that you want to calculate an equation that you can't know until you've received it?

if so see Calculate string value in javascript, not using eval .

in short:

eval CAN be used sometimes, but only if the equation string comes from a trusted source, and there you need something like evaluating dynamic equations.

like image 23
user2264587 Avatar answered Oct 10 '22 23:10

user2264587