node.js - Nodejs 中的自定义 REPL

标签 node.js read-eval-print-loop

我正在尝试使用文档中 REPL 中内置的 Nodejs。

http://nodejs.org/api/repl.html

添加项目的示例如下:

repl.start().context.m = msg;

我似乎无法找到添加多个菜单的方法。我尝试过这样做:

menus = {m = 'hello', f = 'foo'}
repl.start().context = menus

但这也行不通。我得到:

testREPL> m
TypeError: needs a 'context' argument.
    at REPLServer.self.eval (repl.js:113:21)
    at Interface.<anonymous> (repl.js:250:12)
    at Interface.EventEmitter.emit (events.js:88:17)
    at Interface._onLine (readline.js:199:10)
    at Interface._normalWrite._line_buffer (readline.js:308:12)
    at Array.forEach (native)
    at Interface._normalWrite (readline.js:307:11)
    at Socket.ondata (readline.js:90:10)
    at Socket.EventEmitter.emit (events.js:115:20)
    at TCP.onread (net.js:395:14)

有人知道如何让它工作吗?

最佳答案

您无法分配给 context 属性,您必须向其添加属性。您正在尝试的是用您自己的对象“覆盖”它。尝试单独分配每个属性:

var context = repl.start({}).context;
context.m = 'hello';
context.f = 'foo';

关于node.js - Nodejs 中的自定义 REPL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12455708/

相关文章:

recursion - 达到基本情况后无法从 Prolog 中的递归中获取结果

ruby - pry ( ruby REPL): How can I turn automatic echo off

node.js - 如何使用 Redis 以原子方式增加值

javascript - 如何在 Nodejs 中将整数数组作为字节流写入客户端?

javascript - REPL 中++ 和 += 1 的区别

javascript - 在没有实例的情况下结束 REPL 服务器

clojure - 为什么不能在 Clojure REPL 中更改 clojure.repl/print-doc 绑定(bind)?

javascript - 写一个 webpack 加载器——一次注入(inject)代码

node.js - 如何在 NGINX 服务器上使用 Meteor 服务多个 React Native 应用程序?

javascript - HapiJS 启动更长的后台进程