我想启动一个交互式节点shell,其中一些对象已经初始化。但是,以下代码会导致:
var repl = require('repl')
var x = 11,
y = 21
var con = {}
con.x = x
con.y = y
repl.start('> ').context = con
当我开始尝试访问其中一个上下文变量时会发生这种情况:
node .
> y
TypeError: sandbox argument must have been converted to a context.
at TypeError (native)
at REPLServer.defaultEval (repl.js:250:27)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:412:12)
at emitOne (events.js:77:13)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:826:14)
然而repl.start('> ').context.y = y
有效。
我做错了什么?
答案 0 :(得分:1)
Object.assign(repl.start('> ').context, con)
键入repl.start('> ').context
时,对象非常大。我想将context
分配给自定义对象会遗漏一些属性。