简体   繁体   English

在node.js中链接underscore.js会抛出'Invalid REPL keyword'

[英]Chaining underscore.js in node.js throws 'Invalid REPL keyword'

I'm a n00b at using underscore/node and am trying to understand the concept of chaining functions. 我是使用下划线/节点的n00b,我试图理解链接函数的概念。 However, I cannot elicit proper outputs when trying to chain functions in node. 但是,在尝试链接节点中的函数时,我无法获得正确的输出。 Grabbing the example snipp from underscore's chaining section produces 'Invalid REPL keyword': 从下划线的链接部分抓取示例snipp会生成“Invalid REPL keyword”:

var __ = require("underscore"); //for underscore use in node

var lyrics = [
  {line: 1, words: "I'm a lumberjack and I'm okay"},
  {line: 2, words: "I sleep all night and I work all day"},
  {line: 3, words: "He's a lumberjack and he's okay"},
  {line: 4, words: "He sleeps all night and he works all day"}
];

__.chain(lyrics) //in the console chain appears to run and return correctly, but then
  .map(function(line) { return line.words.split(' '); }) //Invalid REPL keyword
  .flatten()                                             //Invalid REPL keyword
  .reduce(function(counts, word) { 
    counts[word] = (counts[word] || 0) + 1;
    return counts;
  }, {})                                                 //Invalid REPL keyword
  .value();                                              //Invalid REPL keyword

Am I a victim of ASI in this case? 在这种情况下,我是ASI的受害者吗? If so where is the ';' 如果是这样的话';' trying to be inserted? 试图插入? I am confused since plugging this snippet into JSHint produces no errors. 我很困惑,因为将此片段插入JSHint不会产生任何错误。 Can one of you please help me identify the error? 你们其中一个人可以帮我识别错误吗?

Thanks! 谢谢!

I guess in a way you are running into ASI in that generally semicolons are inserted to separate expressions. 我猜你在某种程度上遇到了ASI,因为通常会将分号插入到单独的表达式中。 In a more general sense though, what you are doing is typing into Node's REPL (what you get when you run node with no args), and in a REPL environment, if a line can be executed on its own, it will and its result will be printed out. 但是在更一般的意义上,你正在做的是输入Node的REPL (当你运行没有args的node时得到的东西),并且在REPL环境中,如果一行可以自己执行,它将会及其结果将被打印出来。

This is different from a standard JS environment, which will fully process an entire function/file before executing it. 这与标准JS环境不同,后者将在执行之前完全处理整个函数/文件。

The error you are getting, Invalid REPL keyword , is because Node's REPL has a set of commands that all begin with . 你得到的错误, Invalid REPL keyword ,是因为Node的REPL有一组所有开头的命令. , such as .clear ( the full list is here ), and .map and such are JS functions, not REPL commands. 例如.clear完整列表在这里 )和.map等是JS函数,而不是REPL命令。

So for example if I take your example and reorder the . 所以,例如,如果我拿你的例子并重新排序. s to the end of the line (so that each line can't be processed on its own), it will work in the REPL: s到行的末尾(这样每行不能自己处理),它将在REPL中工作:

var __ = require("underscore"); //for underscore use in node

var lyrics = [
  {line: 1, words: "I'm a lumberjack and I'm okay"},
  {line: 2, words: "I sleep all night and I work all day"},
  {line: 3, words: "He's a lumberjack and he's okay"},
  {line: 4, words: "He sleeps all night and he works all day"}
];

__.chain(lyrics).
  map(function(line) {
    return line.words.split(' ');
  }).
  flatten().
  reduce(function(counts, word) { 
    counts[word] = (counts[word] || 0) + 1;
    return counts;
  }, {}).
  value();

Really though, the only time you should be using the REPL is for quick tests, where the single-line behavior is usually helpful. 实际上,您应该使用REPL的唯一时间是快速测试,其中单行行为通常很有帮助。 When doing normal development, you should work in a file and run node <filename> to test your code. 在进行正常开发时,您应该在一个文件中运行并运行node <filename>来测试您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM