I've been learning about node.js and modules, and can't seem to get the Underscore library to work properly... it seems that the first time I use a function from Underscore, it overwrites the _ object with the result of my function call. Anyone know what's going on? For example, here is a session from the node.js REPL:
我一直在学习node。js和模块,下划线库似乎无法正常工作……看起来我第一次使用一个下划线的函数时,它用函数调用的结果覆盖了_对象。有人知道发生了什么吗?例如,这是来自节点的会话。js REPL:
Admin-MacBook-Pro:test admin$ node
> require("./underscore-min")
{ [Function]
_: [Circular],
VERSION: '1.1.4',
forEach: [Function],
each: [Function],
map: [Function],
inject: [Function],
(...more functions...)
templateSettings: { evaluate: /<%([\s\S]+?)%>/g, interpolate: /<%=([\s\S]+?)%>/g },
template: [Function] }
> _.max([1,2,3])
3
> _.max([4,5,6])
TypeError: Object 3 has no method 'max'
at [object Context]:1:3
at Interface.<anonymous> (repl.js:171:22)
at Interface.emit (events.js:64:17)
at Interface._onLine (readline.js:153:10)
at Interface._line (readline.js:408:8)
at Interface._ttyWrite (readline.js:585:14)
at ReadStream.<anonymous> (readline.js:73:12)
at ReadStream.emit (events.js:81:20)
at ReadStream._emitKey (tty_posix.js:307:10)
at ReadStream.onData (tty_posix.js:70:12)
> _
3
When I make Javascript files myself and import them, they seem to be working properly. Maybe there's something special with the Underscore library?
当我自己制作并导入Javascript文件时,它们似乎运行正常。也许下划线库有什么特别之处?
5 个解决方案
#1
167
The Node REPL uses the underscore variable to hold the result of the last operation, so it conflicts with the Underscore library's use of the same variable. Try something like this:
节点REPL使用下划线变量来保存最后一次操作的结果,因此它与下划线库对同一变量的使用发生冲突。试试这样:
Admin-MacBook-Pro:test admin$ node
> _und = require("./underscore-min")
{ [Function]
_: [Circular],
VERSION: '1.1.4',
forEach: [Function],
each: [Function],
map: [Function],
inject: [Function],
(...more functions...)
templateSettings: { evaluate: /<%([\s\S]+?)%>/g, interpolate: /<%=([\s\S]+?)%>/g },
template: [Function] }
> _und.max([1,2,3])
3
> _und.max([4,5,6])
6
#2
193
As of today (April 30, 2012) you can use Underscore as usual on your Node.js code. Previous comments are right pointing that REPL interface (Node's command line mode) uses the "_" to hold the last result BUT on you are free to use it on your code files and it will work without a problem by doing the standard:
从今天(2012年4月30日)开始,您可以像往常一样在节点上使用下划线。js代码。前面的评论指出,REPL接口(Node的命令行模式)使用“_”来保存最后的结果,但是您可以在您的代码文件上使用它,它将通过执行标准而毫无问题地工作:
var _ = require('underscore');
Happy coding!
编码快乐!
#3
28
Or :
或者:
var _ = require('underscore')._;
#4
13
The name _
used by the node.js
REPL to hold the previous input. Choose another name.
节点使用的名称_。js REPL用于保存先前的输入。选择另一个名称。
#5
-2
Note: The following only works for the next line of code, and only due to a coincidence.
注意:下面的代码只适用于下一行代码,这仅仅是出于巧合。
With Lodash,
Lodash,
require('lodash');
_.isArray([]); // true
No var _ = require('lodash')
since Lodash mysteriously sets this value globally when required.
没有var _ = require('lodash'),因为当需要时,lodash会神秘地全局地设置这个值。
#1
167
The Node REPL uses the underscore variable to hold the result of the last operation, so it conflicts with the Underscore library's use of the same variable. Try something like this:
节点REPL使用下划线变量来保存最后一次操作的结果,因此它与下划线库对同一变量的使用发生冲突。试试这样:
Admin-MacBook-Pro:test admin$ node
> _und = require("./underscore-min")
{ [Function]
_: [Circular],
VERSION: '1.1.4',
forEach: [Function],
each: [Function],
map: [Function],
inject: [Function],
(...more functions...)
templateSettings: { evaluate: /<%([\s\S]+?)%>/g, interpolate: /<%=([\s\S]+?)%>/g },
template: [Function] }
> _und.max([1,2,3])
3
> _und.max([4,5,6])
6
#2
193
As of today (April 30, 2012) you can use Underscore as usual on your Node.js code. Previous comments are right pointing that REPL interface (Node's command line mode) uses the "_" to hold the last result BUT on you are free to use it on your code files and it will work without a problem by doing the standard:
从今天(2012年4月30日)开始,您可以像往常一样在节点上使用下划线。js代码。前面的评论指出,REPL接口(Node的命令行模式)使用“_”来保存最后的结果,但是您可以在您的代码文件上使用它,它将通过执行标准而毫无问题地工作:
var _ = require('underscore');
Happy coding!
编码快乐!
#3
28
Or :
或者:
var _ = require('underscore')._;
#4
13
The name _
used by the node.js
REPL to hold the previous input. Choose another name.
节点使用的名称_。js REPL用于保存先前的输入。选择另一个名称。
#5
-2
Note: The following only works for the next line of code, and only due to a coincidence.
注意:下面的代码只适用于下一行代码,这仅仅是出于巧合。
With Lodash,
Lodash,
require('lodash');
_.isArray([]); // true
No var _ = require('lodash')
since Lodash mysteriously sets this value globally when required.
没有var _ = require('lodash'),因为当需要时,lodash会神秘地全局地设置这个值。