Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore doesn't work in Coffeescript's console

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
like image 372
KajMagnus Avatar asked Jun 11 '12 02:06

KajMagnus


People also ask

What is-> in CoffeeScript?

In CoffeeScript there are two different types of arrows for defining functions: thin arrows -> and fat arrows =>. The JavaScript function keyword was replaced with the thin arrow. The fat arrow serves as the function keyword and also binds the function to the current context.

How to comment in CoffeeScript?

CoffeeScript Comments Whenever we want to comment a single line in CoffeeScript, we just need to place a hash tag before it as shown below. Every single line that follows a hash tag (#) is considered as a comment by the CoffeeScript compiler and it compiles the rest of the code in the given file except the comments.

What can you do with CoffeeScript?

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.


1 Answers

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.

like image 70
mu is too short Avatar answered Oct 05 '22 17:10

mu is too short