下划线在 Coffeescript 的控制台中不起作用 [英] Underscore doesn't work in Coffeescript's console
问题描述
我刚刚开始使用 Coffeescript 和 Coffeescript 控制台,以及 Underscore.但是,每当我定义一个函数时,Coffeescript 都会确定 _
表示该函数,并且似乎忘记了 _ = require 'underscore'
的初始分配.
I've just started using Coffeescript and the Coffeescript console, and Underscore. However, whenever I define a function, Coffeescript decides that _
means that function, and seems to forget the initial assignment of _ = require 'underscore'
.
为什么会这样?如何预防?
(我真的很希望能够将粘贴代码从我的文件复制到控制台中.)
Why does this happen? How can I prevent it?
(I'd really like to be able to copy paste code from my files into the console.)
(_
在 Coffeescript 控制台中有什么特殊含义吗?它的意思是最后的结果"还是什么?这可以解释我的问题吗?)
(Does _
have some special meaning in the Coffeescript console? Does it mean "the last result" or something? That'd explain my issue?)
详情:
$ 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
推荐答案
CoffeeScript REPL 有 这个 JavaScript 的核心:
The CoffeeScript REPL has this JavaScript at its heart:
try {
_ = global._;
returnValue = CoffeeScript["eval"]("_=(" + code + "
)", {
filename: 'repl',
modulename: 'repl'
});
if (returnValue === void 0) {
global._ = _;
}
repl.output.write("" + (inspect(returnValue, false, 2, enableColours)) + "
");
} catch (err) {
error(err);
}
所以如果最后一个命令返回了一些东西,那么 _
就是那个东西.虽然我找不到任何关于此的文档,但搜索 _
并不是一项非常有成效的活动.如果您想在 CoffeeScript REPL 中使用 Underscore.js,则必须将其命名为 _
以外的其他名称.
So if the last command returned something then _
will be that something. I can't find any documentation about this though but searching for _
isn't a terribly productive activity. If you want to use Underscore.js in the CoffeeScript REPL, you'll have to call it something other than _
.
感谢 Trevor Burnham(谁写了 这本书 所以我认为我们可以信任他)我们知道 CoffeeScript REPL 使用 _
作为匹配node.js REPL 的行为:
Thanks to Trevor Burnham (who wrote the book so I think we can trust him on this) we know that the CoffeeScript REPL uses _
as the last result to match the behavior of the node.js REPL:
REPL 功能
[...]
特殊变量 _
(下划线)包含最后一个表达式的结果.
REPL Features
[...]
The special variable_
(underscore) contains the result of the last expression.
Ruby 的 irb
做同样的事情.
Ruby's irb
does the same thing.
这篇关于下划线在 Coffeescript 的控制台中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!