0
I came across the functionalities of eval()
and I started using, but I learned about the risk in using eval()
next to a input external.
I use the eval()
to receive from a input-text to string which corresponds to a polynomial function. It was the most practical short-term alternative that I managed to pass a user data as variables and mathematical operations.
Are there risks in hosting a code with this function in the future? Is there an alternative to the eval()
in that capacity?
function inserirLinhaTabela()
{
//Cálculo numérico:
var poli = document.getElementById("fx").value;
var troca =
{
sen: "Math.sin",
cos: "Math.cos",
e: "Math.E",
ln: "Math.log",
}
poli = poli.replace(/sen|cos|e|ln/gi, function(matched){
return troca[matched];
});
function fdex(x)
{
return eval(poli);
}
It is a good initiative to ask how to avoid an Eval, but the essential has already been said in the comments of your previous question. The answer given here is an excellent starting point for implementing the suggested parser. The following research can be used as a starting point: https://www.google.com/search?q=javascript+polynomial+parser
– Bacco