在 Node.js REPL 中,为什么会发生这种情况? [英] In the Node.js REPL, why does this happen?

查看:67
本文介绍了在 Node.js REPL 中,为什么会发生这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以当我发现一些非常奇怪的东西时,我正在尝试使用 Node.js REPL 和 Underscore 库.如果我 require("underscore"),变量 _ 是全局设置的(显然).然后,当我尝试运行像 console.log(_.isEmpty) 这样的简单命令时,它会打印 [Function] (同样,显然).然而,在运行 console.log(_) 之后,它会打印 [Function] 因为变量 _ 被设置为 _.isEmpty.

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.

为什么要这样做?如果我从 js 文件运行相同的代码,则不会发生这种情况.这是正常的 Node 事情还是完全错误?

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?

仅供参考:节点 v0.10.10

FYI: Node v0.10.10

推荐答案

Node 的 REPL 总是将 _ 设置为最后一行的结果.

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

这篇关于在 Node.js REPL 中,为什么会发生这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆