返回作为字符串参数传递的值时使用 eval() 的正确方法
Correct method of using eval() when returning values passed as string parameters
我将 string 参数传递给 calculate() 函数。我需要将它们作为计算值返回。我在以下哪个表格中正确使用了 eval()?
数据:
var nums = ['2','3','1'], sum = ['+'];
第一个版本:
function calculate(a,b,c,d,e)
{
console.log('Calculating...(plus separators): '+a+' '+b+' '+c+' '+d+' '+e);
console.log('Calculating...(comma separators): ',a,' ',b,' ',c,' ',d,' ',e);
console.log('a :',a);
console.log('b :',b);
console.log('c :',c);
console.log('d :',d);
console.log('e :',e);
return eval(a+b+c+d+e);
};
console.log('RESULT: ',calculate(nums[0],sum[0], nums[1],sum[0], nums[2]));
第二个版本:
function calculateP(a,c,e)
{
console.log('Calculating...(plus separators): '+a+' '+c+' '+e);
console.log('Calculating...(comma separators): ',a,' ',c,' ',e);
console.log('a: ',a);
console.log('c: ',c);
console.log('e: ',e);
return eval(a+c+e);
};
console.log('precise RESULT: ',calculateP(nums[0], nums[1], nums[2]));
控制台 显示 "RESULT: 6" 和 "precise RESULT: 231".
哪一个是正确的,如果有的话?在我看来,第一个 ("RESULT 6") 没问题,但我宁愿确定。
P.S 我使用了 eval(),因为最初我会将数学表达式作为字符串 "2+3 -(4*3)/7" 并希望对其进行计算。某种简单的计算器解析器
P.S #2 我使用了数组,因为最终我要将更多的数组元素传递给这个函数。
__________________________________________________________________________
编辑
现在正确使用了吗eval()?
function plus(a,b) {
return a+b;
}
function minus(a,b) {
return a-b;
}
var expression = 'minus(plus(2, 3),minus(5,3))';
console.log('(2+3)-(5-3) ',eval(expression));
expression = 'plus(plus(2, 3),minus(5,3))';
console.log('(2+3)+(5-3) ',eval(expression));
您好,您必须像下面这样使用:
var nums = ['2','3','1'], sum = ['+'];
function calculate(nums)
{
var res =0;
for(var i=0;i<nums.length;i++){
res= res+(parseInt(nums[i]));
}
return res;
};
console.log('RESULT: ',calculate(nums))
我真的相信你几乎可以在任何地方避免eval
,但如果你做坏事 - 做好!
var expression = '2 + 3 - (4 * 3) / 7';
console.log(eval(expression));
数学运算?简单!
function cos(input) {
return Math.cos(input);
}
function ln(input) {
return Math.log(input);
}
var expression = '2 + 3 - cos(4 * 3) / ln(7)';
console.log(eval(expression));
你用 eval 做的事情绝对很奇怪,违背了 eval 的本性。
只是一个例子,如何在没有 eval()
的情况下处理字符串和一些值。
var operators = {
'+': function (a, i) {
a[i - 1] += a.splice(i, 2)[1];
return true;
},
'*': function (a, i) {
a[i - 1] *= a.splice(i, 2)[1];
return true;
},
'(': function (a, i) {
var j = i + 1,
b;
while (j < a.length) {
if (a[j] === ')') {
b = a.splice(i + 1, j - i);
b.pop();
a[i] = calculate(b);
return true;
}
if (a[j] === '(') {
return false;
}
j++;
}
},
')': 0
},
precedence = ['(', '*', '+'];
function split(s) {
var a = s.split(''),
i = 1;
while (i < a.length) {
if (!(a[i - 1] in operators || a[i] in operators)) {
a[i - 1] += a.splice(i, 1)[0];
continue;
}
i++;
}
return a.map(function (b) {
return b in operators ? b : Number(b);
});
}
function calculate(a) {
while (a.length > 1) {
precedence.some(function (b) {
return a.some(function (c, i) {
if (b === c) {
return operators[b](a, i);
}
});
});
}
return a[0];
}
document.write(calculate(split('12+23*37')) + '<br>'); // 863
document.write(calculate(split('12+23*(2+(3*4)+5)*37')) + '<br>'); // 16181
我将 string 参数传递给 calculate() 函数。我需要将它们作为计算值返回。我在以下哪个表格中正确使用了 eval()?
数据:
var nums = ['2','3','1'], sum = ['+'];
第一个版本:
function calculate(a,b,c,d,e)
{
console.log('Calculating...(plus separators): '+a+' '+b+' '+c+' '+d+' '+e);
console.log('Calculating...(comma separators): ',a,' ',b,' ',c,' ',d,' ',e);
console.log('a :',a);
console.log('b :',b);
console.log('c :',c);
console.log('d :',d);
console.log('e :',e);
return eval(a+b+c+d+e);
};
console.log('RESULT: ',calculate(nums[0],sum[0], nums[1],sum[0], nums[2]));
第二个版本:
function calculateP(a,c,e)
{
console.log('Calculating...(plus separators): '+a+' '+c+' '+e);
console.log('Calculating...(comma separators): ',a,' ',c,' ',e);
console.log('a: ',a);
console.log('c: ',c);
console.log('e: ',e);
return eval(a+c+e);
};
console.log('precise RESULT: ',calculateP(nums[0], nums[1], nums[2]));
控制台 显示 "RESULT: 6" 和 "precise RESULT: 231".
哪一个是正确的,如果有的话?在我看来,第一个 ("RESULT 6") 没问题,但我宁愿确定。
P.S 我使用了 eval(),因为最初我会将数学表达式作为字符串 "2+3 -(4*3)/7" 并希望对其进行计算。某种简单的计算器解析器
P.S #2 我使用了数组,因为最终我要将更多的数组元素传递给这个函数。
__________________________________________________________________________
编辑
现在正确使用了吗eval()?
function plus(a,b) {
return a+b;
}
function minus(a,b) {
return a-b;
}
var expression = 'minus(plus(2, 3),minus(5,3))';
console.log('(2+3)-(5-3) ',eval(expression));
expression = 'plus(plus(2, 3),minus(5,3))';
console.log('(2+3)+(5-3) ',eval(expression));
您好,您必须像下面这样使用:
var nums = ['2','3','1'], sum = ['+'];
function calculate(nums)
{
var res =0;
for(var i=0;i<nums.length;i++){
res= res+(parseInt(nums[i]));
}
return res;
};
console.log('RESULT: ',calculate(nums))
我真的相信你几乎可以在任何地方避免eval
,但如果你做坏事 - 做好!
var expression = '2 + 3 - (4 * 3) / 7';
console.log(eval(expression));
数学运算?简单!
function cos(input) {
return Math.cos(input);
}
function ln(input) {
return Math.log(input);
}
var expression = '2 + 3 - cos(4 * 3) / ln(7)';
console.log(eval(expression));
你用 eval 做的事情绝对很奇怪,违背了 eval 的本性。
只是一个例子,如何在没有 eval()
的情况下处理字符串和一些值。
var operators = {
'+': function (a, i) {
a[i - 1] += a.splice(i, 2)[1];
return true;
},
'*': function (a, i) {
a[i - 1] *= a.splice(i, 2)[1];
return true;
},
'(': function (a, i) {
var j = i + 1,
b;
while (j < a.length) {
if (a[j] === ')') {
b = a.splice(i + 1, j - i);
b.pop();
a[i] = calculate(b);
return true;
}
if (a[j] === '(') {
return false;
}
j++;
}
},
')': 0
},
precedence = ['(', '*', '+'];
function split(s) {
var a = s.split(''),
i = 1;
while (i < a.length) {
if (!(a[i - 1] in operators || a[i] in operators)) {
a[i - 1] += a.splice(i, 1)[0];
continue;
}
i++;
}
return a.map(function (b) {
return b in operators ? b : Number(b);
});
}
function calculate(a) {
while (a.length > 1) {
precedence.some(function (b) {
return a.some(function (c, i) {
if (b === c) {
return operators[b](a, i);
}
});
});
}
return a[0];
}
document.write(calculate(split('12+23*37')) + '<br>'); // 863
document.write(calculate(split('12+23*(2+(3*4)+5)*37')) + '<br>'); // 16181