将 Underscore 模块与 Node.js 结合使用

发布于 2024-11-02 01:07:05 字数 1119 浏览 2 评论 0原文

我一直在学习 Node.js 和模块,但似乎无法让 Underscore 库正常工作...似乎我第一次使用 Underscore 中的函数时,它会覆盖 _ 对象,结果为我的函数调用。有人知道发生了什么事吗?例如,这是来自 node.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

当我自己制作 Javascript 文件并导入它们时,它们似乎工作正常。也许 Underscore 库有什么特别之处?

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

老旧海报 2024-11-09 01:07:05

从今天开始(2012 年 4 月 30 日),您可以像往常一样在 Node.js 代码上使用 Underscore。之前的评论是正确的,REPL 接口(Node 的命令行模式)使用“_”来保存最后的结果,但是您可以在代码文件上自由使用它,并且通过执行标准,它将毫无问题地工作:

var _ = require('underscore');

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:

var _ = require('underscore');
画中仙 2024-11-09 01:07:05

Node REPL 使用下划线变量来保存上次操作的结果,因此它与 Underscore 库对同一变量的使用发生冲突。尝试这样的事情:

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

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:

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
暗地喜欢 2024-11-09 01:07:05

或者 :

    var _ = require('underscore')._;

Or :

    var _ = require('underscore')._;
画尸师 2024-11-09 01:07:05

node.js REPL 用于保存先前输入的名称 _。选择另一个名称。

The name _ used by the node.js REPL to hold the previous input. Choose another name.

初熏 2024-11-09 01:07:05

注意:以下内容仅适用于下一行代码,并且只是由于巧合。

对于 Lodash,

require('lodash');
_.isArray([]); // true

没有 var _ = require('lodash') 因为 Lodash 很神秘需要时全局设置该值。

Note: The following only works for the next line of code, and only due to a coincidence.

With Lodash,

require('lodash');
_.isArray([]); // true

No var _ = require('lodash') since Lodash mysteriously sets this value globally when required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文