简体   繁体   English

Node.js REPL中的“_”(下划线)符号是什么意思?

[英]What does the “_” (underscore) symbol in Node.js REPL mean?

I was playing in Node.js with some code when I noticed this thing: 当我注意到这件事时,我正在使用一些代码在Node.js中玩:

> 'hello world'.padEnd(20);
'hello world         '
> 'hello world'.padEnd(20, _);
'hello worldhello wor'

What does the underscore symbol do here? 下划线符号在这里做什么?

> _
'hello worldhello wor'

_ in the node console returns the result of the last expression. _在节点控制台中返回最后一个表达式的结果。

> 1 + 2
3
> _
3

_ symbol returns the result of the last logged expression in REPL node console: _符号返回REPL节点控制台中最后记录的表达式的结果:

> 2 * 2
4
> _
4

As written in documentation , in 6.x and higher versions of node this behavior can be disabled by setting value to _ explicitly: 文档中所述 ,在6.x及更高版本的节点中,可以通过将value显式设置为_来禁用此行为:

> [ 'a', 'b', 'c' ]
[ 'a', 'b', 'c' ]
> _.length
3
> _ += 1
Expression assignment to _ now disabled.
4
> 1 + 1
2
> _
4

But in older versions that feature doesn't work: 但在旧版本中,该功能不起作用:

> [ 'a', 'b', 'c' ]
[ 'a', 'b', 'c' ]
> _.length
3
> _ += 1
4
> 1 + 1
2
> _
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM