简体   繁体   中英

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

I was playing in Node.js with some code when I noticed this thing:

> '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:

> 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:

> [ '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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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