相关疑难解决方法(0)

将Underscore模块与Node.js一起使用

我一直在学习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
Run Code Online (Sandbox Code Playgroud)

当我自己制作Javascript文件并导入它们时,它们似乎正常工作.也许Underscore图书馆有一些特别的东西?

module node.js underscore.js

146
推荐指数
4
解决办法
11万
查看次数

Underscore在Coffeescript的控制台中不起作用

我刚刚开始使用Coffeescript和Coffeescript控制台,以及Underscore.但是,每当我定义一个函数时,Coffeescript就决定了_那个函数,并且似乎忘记了初始赋值_ = require 'underscore'.

为什么会这样?我该怎样预防呢?
(我真的希望能够将我的文件中的粘贴代码复制到控制台中.)

(_在Coffeescript控制台中有一些特殊含义吗?它是指"最后的结果"还是什么?这可以解释我的问题吗?)

细节:

$ coffee 
coffee> _.contains [1, 2, 3], 3   # no Underscore, initially
TypeError: Cannot call method 'contains' of undefined
    at ...
coffee> 
coffee> _ = require 'underscore'
{ [Function]
  _: [Circular],
  VERSION: '1.3.3',
  forEach: [Function],
  ...

coffee> _.contains [1, 2, 3], 3    # now Underscore works fine
true
coffee> 
------> someFunction = (a, b) ->   # define a function ...
......>   a + b

[Function]
coffee> …
Run Code Online (Sandbox Code Playgroud)

coffeescript underscore.js

11
推荐指数
1
解决办法
1661
查看次数

标签 统计

underscore.js ×2

coffeescript ×1

module ×1

node.js ×1