Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...