2012-09-17 66 views
2

我正在嘗試使用從文檔中創建的REPL中的nodejs。nodejs中的自定義REPL

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

您不能分配到context屬性,您必須添加屬性。你試圖用自己的對象「覆蓋」它。嘗試分配每個屬性代替:

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

這工作。我猜測JavaScript變量成爲指針? – Menztrual