如何使用js eval来返回值?

时间:2021-05-03 20:15:16

I need to evaluate a custom function passed from the server as a string. It's all part of a complicated json I get, but anyway, I seem to be needing something along the lines:

我需要评估从服务器传递的自定义函数作为字符串。这是一个复杂的json的一部分,但无论如何,我似乎需要一些类似的东西:

var customJSfromServer = "return 2+2+2;"
var evalValue = eval(customJSfromServer);
alert(evalValue) ;// should be "6";

Obviously this is not working as I expected. Any way I can achieve this ?

很明显,这不是我所期望的。我能做到吗?

6 个解决方案

#1


56  

The first method is to delete return keywords and the semicolon:

第一个方法是删除返回关键字和分号:

var expression = '2+2+2';
var result = eval('(' + expression + ')')
alert(result);

note the '(' and ')' is a must.

注意“(”和“)”是必须的。

or you can make it a function:

或者你可以把它变成一个函数

var expression = 'return 2+2+2;'
var result = eval('(function() {' + expression + '}())');
alert(result);

even simpler, do not use eval:

更简单,不要使用eval:

var expression = 'return 2+2+2;';
var result = new Function(expression)();
alert(result);

#2


6  

If you can guarantee the return statement will always exist, you might find the following more appropriate:

如果您可以保证返回语句始终存在,您可能会发现以下更合适:

var customJSfromServer = "return 2+2+2;"
var asFunc = new Function(customJSfromServer);
alert(asFunc()) ;// should be "6";

Of course, you could also do:

当然,你也可以这样做:

var customJSfromServer = "return 2+2+2;"
var evalValue = (new Function(customJSfromServer)());
alert(evalValue) ;// should be "6";

#3


3  

var customJSfromServer = "2+2+2;"
var evalValue = eval(customJSfromServer);
alert(evalValue) ;// should be "6";

#4


2  

There should not be return statement , as eval will read this as statment and will not return value.

不应该有return语句,因为eval会将其读取为状态,而不会返回值。

var customJSfromServer = "2+2+2;"
var evalValue = eval( customJSfromServer );
alert(evalValue) ;// should be "6";

see http://www.w3schools.com/jsref/jsref_eval.asp

参见http://www.w3schools.com/jsref/jsref_eval.asp

#5


2  

This works:

如此:

function answer() {
    return 42;
}

var a = eval('answer()');
console.log(a);

You need to wrap the return inside a function and it should pass the value on from the eval.

您需要将返回值封装到函数中,并且它应该从eval传递值。

#6


1  

Modify server response to get "2+2+2" (remove "return") and try this:

修改服务器响应以获得“2+2+2”(删除“返回”),并尝试:

var myvar = eval(response);
alert(myvar);

#1


56  

The first method is to delete return keywords and the semicolon:

第一个方法是删除返回关键字和分号:

var expression = '2+2+2';
var result = eval('(' + expression + ')')
alert(result);

note the '(' and ')' is a must.

注意“(”和“)”是必须的。

or you can make it a function:

或者你可以把它变成一个函数

var expression = 'return 2+2+2;'
var result = eval('(function() {' + expression + '}())');
alert(result);

even simpler, do not use eval:

更简单,不要使用eval:

var expression = 'return 2+2+2;';
var result = new Function(expression)();
alert(result);

#2


6  

If you can guarantee the return statement will always exist, you might find the following more appropriate:

如果您可以保证返回语句始终存在,您可能会发现以下更合适:

var customJSfromServer = "return 2+2+2;"
var asFunc = new Function(customJSfromServer);
alert(asFunc()) ;// should be "6";

Of course, you could also do:

当然,你也可以这样做:

var customJSfromServer = "return 2+2+2;"
var evalValue = (new Function(customJSfromServer)());
alert(evalValue) ;// should be "6";

#3


3  

var customJSfromServer = "2+2+2;"
var evalValue = eval(customJSfromServer);
alert(evalValue) ;// should be "6";

#4


2  

There should not be return statement , as eval will read this as statment and will not return value.

不应该有return语句,因为eval会将其读取为状态,而不会返回值。

var customJSfromServer = "2+2+2;"
var evalValue = eval( customJSfromServer );
alert(evalValue) ;// should be "6";

see http://www.w3schools.com/jsref/jsref_eval.asp

参见http://www.w3schools.com/jsref/jsref_eval.asp

#5


2  

This works:

如此:

function answer() {
    return 42;
}

var a = eval('answer()');
console.log(a);

You need to wrap the return inside a function and it should pass the value on from the eval.

您需要将返回值封装到函数中,并且它应该从eval传递值。

#6


1  

Modify server response to get "2+2+2" (remove "return") and try this:

修改服务器响应以获得“2+2+2”(删除“返回”),并尝试:

var myvar = eval(response);
alert(myvar);