Javascript Node.js REPL在函数声明或函数表达式后自动声明下划线?

Javascript Node.js REPL在函数声明或函数表达式后自动声明下划线?,javascript,node.js,Javascript,Node.js,今天我发现node.js有些奇怪: $ node > console.log(_) ReferenceError: _ is not defined at repl:1:13 at REPLServer.defaultEval (repl.js:130:27) at bound (domain.js:257:14) at REPLServer.runBound [as eval] (domain.js:270:12) at REPLServer.&l

今天我发现node.js有些奇怪:

$ node
> console.log(_)
ReferenceError: _ is not defined
    at repl:1:13
    at REPLServer.defaultEval (repl.js:130:27)
    at bound (domain.js:257:14)
    at REPLServer.runBound [as eval] (domain.js:270:12)
    at REPLServer.<anonymous> (repl.js:277:12)
    at REPLServer.EventEmitter.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:202:10)
    at REPLServer.Interface._line (readline.js:531:8)
    at REPLServer.Interface._ttyWrite (readline.js:812:14)
    at ReadStream.onkeypress (readline.js:101:10)
> function foo() {}
undefined
> console.log(_)
undefined
undefined
$node
>console.log(41;
ReferenceError:\未定义
回复:1:13
在REPLServer.defaultEval(repl.js:130:27)
绑定时(domain.js:257:14)
在REPLServer.runBound[as eval](domain.js:270:12)
在服务器上。(回复js:277:12)
在REPLServer.EventEmitter.emit上(events.js:107:17)
在REPLServer.Interface.\u在线(readline.js:202:10)
在REPLServer.Interface.\u行(readline.js:531:8)
在REPLServer.Interface.\u ttyWrite(readline.js:812:14)
在ReadStream.onkeypress(readline.js:101:10)
>函数foo(){}
未定义
>console.log(41;
未定义
未定义
创建函数表达式后也会发生同样的情况:

$ node
> console.log(_)
ReferenceError: _ is not defined
    at repl:1:13
    at REPLServer.defaultEval (repl.js:130:27)
    at bound (domain.js:257:14)
    at REPLServer.runBound [as eval] (domain.js:270:12)
    at REPLServer.<anonymous> (repl.js:277:12)
    at REPLServer.EventEmitter.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:202:10)
    at REPLServer.Interface._line (readline.js:531:8)
    at REPLServer.Interface._ttyWrite (readline.js:812:14)
    at ReadStream.onkeypress (readline.js:101:10)
> (function () {}())
undefined
> console.log(_)
undefined
undefined
$node
>console.log(41;
ReferenceError:\未定义
回复:1:13
在REPLServer.defaultEval(repl.js:130:27)
绑定时(domain.js:257:14)
在REPLServer.runBound[as eval](domain.js:270:12)
在服务器上。(回复js:277:12)
在REPLServer.EventEmitter.emit上(events.js:107:17)
在REPLServer.Interface.\u在线(readline.js:202:10)
在REPLServer.Interface.\u行(readline.js:531:8)
在REPLServer.Interface.\u ttyWrite(readline.js:812:14)
在ReadStream.onkeypress(readline.js:101:10)
>(函数(){}())
未定义
>console.log(41;
未定义
未定义
它非常酷,而且对于有意保留为未定义的函数参数非常方便,但是为什么会发生这种情况呢?我正在Arch Linux上使用节点版本
v0.11.13

它的一个功能是:
\u
返回最后一个表达式的结果

当然,在某些情况下(如您的console.log),表达式不会返回结果。但这是一种获取最后一个表达式值的简便方法


当然,这只发生在REPL中-如果您键入相同的节点程序并从文件中运行它,
将在您的控制下。

当您试图使用期望
为下划线库的代码时,整个
事情令人沮丧。这些变通办法很老套,根本不起作用;我重新编译以摆脱它--反正我也不使用它。
@DaveNewton但repl不是只有在通过命令行使用node时才起作用吗?不在程序本身中?我很困惑,因为我在REPL文档中遇到了这一行“通过执行node而不使用命令行中的任何参数,您将被放到REPL中”,这也是因为我注意到了这一特性。否则我不会知道或质疑这件事。@I_调试_一切我不知道你的意思。如果您键入
node
,您将得到repl。但是,您可以将任意代码加载到repl中,或者剪切和粘贴等。如果您的代码包含
\uu
符号,则情况会非常糟糕。