node.js - 在 Node.js 中使用 Underscore 模块

标签 node.js module underscore.js

我一直在学习 node.js 和模块,但似乎无法让 Underscore 库正常工作......似乎我第一次使用 Underscore 中的函数时,它会覆盖 _ 对象我的函数调用的结果。有谁知道发生了什么?例如,这是来自 node.js REPL 的 session :

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 库有什么特别之处?

最佳答案

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

var _ = require('underscore');

关于node.js - 在 Node.js 中使用 Underscore 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5691901/

相关文章:

javascript - 按属性查找重复对象并使用 Javascript 或 UnderscoreJS 合并

javascript - 如何从 Outlook 网页版获取 GAL

javascript - 在下划线模板中动态设置图像

node.js - 使用 GitHub 策略时 Passport req.user 未定义

sql - ConcatRelated() 函数在表单上提供唯一值

python : import module once for a whole package

javascript - 如何防止 JS 代码被编译器解析/评估?

javascript - 我如何像 Elm 那样在 JS 中传递函数?

javascript - 使用 Node Bluebird 。 API返回信息太晚

javascript - 是否有 Mongoose 连接错误回调