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'
2 个解决方案
#1
5
_
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及更高版本的节点中,可以通过将值显式设置为_来禁用此行为:
> [ '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
#2
24
_
in the node console returns the result of the last expression.
_在节点控制台中返回最后一个表达式的结果。
> 1 + 2
3
> _
3
#1
5
_
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及更高版本的节点中,可以通过将值显式设置为_来禁用此行为:
> [ '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
#2
24
_
in the node console returns the result of the last expression.
_在节点控制台中返回最后一个表达式的结果。
> 1 + 2
3
> _
3