在 Node.js 中使用 Underscore 模块 [英] Using the Underscore module with Node.js

查看:40
本文介绍了在 Node.js 中使用 Underscore 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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: /<%([sS]+?)%>/g, interpolate: /<%=([sS]+?)%>/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 库有什么特别之处?

When I make Javascript files myself and import them, they seem to be working properly. Maybe there's something special with the Underscore library?

推荐答案

Node REPL 使用 underscore 变量来保存上次操作的结果,因此与 Underscore 库使用相同变量的方式冲突.尝试这样的事情:

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: /<%([sS]+?)%>/g, interpolate: /<%=([sS]+?)%>/g },
  template: [Function] }
> _und.max([1,2,3])
3
> _und.max([4,5,6])
6

这篇关于在 Node.js 中使用 Underscore 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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