表达式eval("function(x){return x * x}")中断了node.js控制台 [英] Expression eval("function(x) { return x*x}") breaks node.js console
问题描述
当我在node.js控制台中键入以下内容时:
When I type something like this in node.js console:
var f = eval("function(x) { return x*x}");
它不计算表达式,并等待我键入其他内容.
It doesn't evaluate the expression and waits for me to type something else.
为什么会这样?为什么我不能在此表达式之后输入另一个表达式?
Why is this happening? Why can't I type another expression after this one?
推荐答案
function(x) { return x*x}
是错误(在控制台中键入以进行检查),因此Node的REPL等待更多.
is an error (type it in the console to check that), so Node's REPL waits for more.
如果要构建和分配函数,则必须评估一个表达式,即返回值的语句.通常的解决方案是用括号关闭函数表达式.
If you want to build and assign the function, you must eval an expression, that is a statement returning a value. The usual solution is to close the function expression with parenthesis.
您可以写
var f = eval("(function(x) { return x*x})");
或
var f = Function("x", "return x*x");
当然
var f = function(x) { return x*x};
但是我想你知道这一点.
but I suppose you know this one.
这篇关于表达式eval("function(x){return x * x}")中断了node.js控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!