Underscore在Coffeescript的控制台中不工作 [英] Underscore doesn't work in Coffeescript's console

查看:106
本文介绍了Underscore在Coffeescript的控制台中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Coffeescript和Coffeescript控制台,以及Underscore。但是,每当我定义一个函数,Coffeescript决定 _ 意味着该函数,似乎忘记了 _ = require'underscore' / code>。



为什么会发生这种情况?我如何防止它?

(我真的很喜欢将粘贴代码从我的文件复制到控制台。)



_ 在Coffeescript控制台中有一些特殊的含义吗?这意味着最后一个结果还是什么?这将解释我的问题?)



详情:

  $ coffee 
coffee> _.contains [1,2,3],3#no Underscore,最初
TypeError:不能调用未定义的方法'contains'
at ...
coffee>
coffee> _ = require'underscore'
{[Function]
_:[Circular],
VERSION:'1.3.3',
forEach: ...

咖啡> _.contains [1,2,3],3#now Underscore工作正常
true
coffee>
------> someFunction =(a,b) - > #define a function ...
......> a + b

[函数]
咖啡>
coffee> _.contains [1,2,3],3#现在`_`不是Underscore了!
TypeError:Object function(a,b){#``_`意思是最后结果还是sth?
return a + b;
}在eval.each上没有方法'contains'
。< anonymous>:3:7 Object.eval(/ usr / local / lib / node_modules / coffee-script / coffee-script.js:142:17)
at interface。< anonymous> (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:131:40)
在Interface.emit(events.js:67:17)
at Interface._onLine(readline.js:162:10)
在Interface._line(readline.js:426:8)
在Interface._ttyWrite(readline.js:603:14)
在ReadStream。< anonymous> (readline.js:82:12)
在ReadStream.emit(events.js:88:20)
在ReadStream._emitKey(tty.js:327:10)
coffee>
coffee> _ = required'underscore'
coffee> _.contains [1,2,3],3#现在都再好一会儿了
true


解决方案

CoffeeScript REPL具有这个JavaScript 的核心:

  try {
_ = global._;
returnValue = CoffeeScript [eval](_ =(+ code +\\\
),{
filename:'repl',
modulename:'repl'
});
if(returnValue === void 0){
global._ = _;
}
repl.output.write(+(inspectValue,false,2,enableColours))+\\\
);
} catch(err){
error(err);
}

因此,如果最后一个命令返回了 _ 将是那个东西。我找不到任何有关这方面的文档,但搜索 _ 不是一个非常有效的活动。如果你想在CoffeeScript REPL中使用Underscore.js,你必须把它叫做 _



感谢 Trevor Burnham (谁写了这本书,所以我想我们可以相信他的)我们知道CoffeeScript REPL使用 _ 作为最后的结果匹配 node.js REPL 的行为:


REPL功能

[...]

特殊变量 _


做同样的事情。


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.)

(Does _ have some special meaning in the Coffeescript console? Does it mean "the last result" or something? That'd explain my issue?)

Details:

$ 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

解决方案

The CoffeeScript REPL has this JavaScript at its heart:

try {
  _ = global._;
  returnValue = CoffeeScript["eval"]("_=(" + code + "\n)", {
    filename: 'repl',
    modulename: 'repl'
  });
  if (returnValue === void 0) {
    global._ = _;
  }
  repl.output.write("" + (inspect(returnValue, false, 2, enableColours)) + "\n");
} catch (err) {
  error(err);
}

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 _.

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 Features
[...]
The special variable _ (underscore) contains the result of the last expression.

Ruby's irb does the same thing.

这篇关于Underscore在Coffeescript的控制台中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆