在Node.js中使用Underscore模块

杰夫

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

迈克·斯科特

Node REPL使用underscore变量保存上一次操作的结果,因此它与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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在electronic中使用node.js模块

在node.js中使用异步模块

如何在Node.js Lambda中使用请求模块

限制在Node.js中使用模块导出

是否可以从 Java 中使用基于 Node.JS 的模块?

使用Node.Js模块,如何在同一模块中使用对象函数?

使用ES模块在Node.js中使用相对路径导入

我可以在Node.js 8中使用ES6模块吗?

如何在某些文件中使用此node.js模块

如何在 Node.Js 应用程序中使用“multer”模块

如何在Node.js中使用repl获取模块功能的详细信息?

我可以在webpack中使用node.js服务器模块吗?

如何在Node JS FS模块中使用Typescript Async / waiting with promise

我如何在Node.js中使用es6模块

在 Node.js 中使用 websockets 时如何保持模块化

在 Laravel 应用程序的公共 JS 文件中使用 Node 模块

我在 node.js 中使用 crypto 模块加密了数据,如何使用 crypto-js 在 react.js 中解密

使用npm安装node.js模块

无法使用Node JS ipp模块打印

使用Express模块安装Node.js

使用Browserify加载Node.js模块

使用请求模块抓取Node.js

使用请求模块的Node.JS代理

Node js 逐行模块

使用node-nan在node.js模块中的SetWindowsHookEx

如何在TypeScript中使用Node`http`模块

在node + express中使用文件上传模块multer

如何正确配置实验性ECMAScript模块,以便可以在Node.js中使用导入/导出

在node.js中使用请求模块时,POST请求状态代码(500)。Postman中的POST请求工作正常