gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:46:14 26 4
gpt4 key购买 nike

我一直在学习 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com