Underscore doesn't work in Coffeescript's console

后端 未结 1 1854
遇见更好的自我
遇见更好的自我 2021-01-17 14:16

I\'ve just started using Coffeescript and the Coffeescript console, and Underscore. However, whenever I define a function, Coffeescript decides that _ means tha

相关标签:
1条回答
  • 2021-01-17 14:27

    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.

    0 讨论(0)
提交回复
热议问题