Expression eval(“function(x) { return x*x}”) breaks node.js console

本秂侑毒 提交于 2019-12-02 01:41:13
function(x) { return x*x}

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.

You may write

var f = eval("(function(x) { return x*x})");

or

var f = Function("x", "return x*x");

and of course

var f = function(x) { return x*x};

but I suppose you know this one.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!