在Node.js中使用Underscore模块

node.js Node.js

番长

2020-04-03

我一直在学习有关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库也许有一些特别之处?

第3898篇《在Node.js中使用Underscore模块》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
猴子 2020.04.03

该名称_由使用node.jsREPL保留以前的输入。选择另一个名称。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android