2

我正在尝试从文档中使用 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)

有谁知道如何让这个工作?

4

1 回答 1

4

您不能分配给该context属性,您必须为其添加属性。您正在尝试用您自己的对象“覆盖”它。尝试自己分配每个属性:

var context = repl.start({}).context;
context.m = 'hello';
context.f = 'foo';
于 2012-09-17T09:15:45.007 回答