我刚刚开始使用 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>
coffee> _.contains [1, 2, 3], 3 # now `_` is not Underscore any more!
TypeError: Object function (a, b) { # Does `_` mean "last result" or sth?
return a + b;
} has no method 'contains'
at evalmachine.<anonymous>:3:7
at Object.eval (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:142:17)
at Interface.<anonymous> (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:131:40)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
at ReadStream._emitKey (tty.js:327:10)
coffee>
coffee> _ = require 'underscore'
coffee> _.contains [1, 2, 3], 3 # Now all is fine again, for a short while
true