In the Node.js REPL, why does this happen?

戏子无情 提交于 2019-12-29 07:42:15

问题


So I was playing around with the Node.js REPL and the Underscore library when I noticed something very strange. If I require("underscore"), the variable _ is set globally (obviously). Then when I attempt to run a simple command like console.log(_.isEmpty) it prints [Function] (again, obviously). However, upon running console.log(_) right after, it prints [Function] because the variable _ was set to _.isEmpty.

Why does this do this? If I run the same code from a js file this doesn't happen. Is this a normal Node thing or is this a total bug?

FYI: Node v0.10.10


回答1:


Node's REPL always sets _ to the result of the last line.

> 2
2
> _
2
> 2+2
4
> _
4
>

You need to use a different identifier:

var u = require("underscore");
u.isEmpty


来源:https://stackoverflow.com/questions/17073290/in-the-node-js-repl-why-does-this-happen

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