将上下文传递给交互式节点shell会导致“TypeError:沙箱参数已转换为上下文”
我想启动一个交互式的节点shell,其中一些对象已经被初始化了。 下面的代码会导致:
var repl = require('repl') var x = 11, y = 21 var con = {} con.x = x con.y = y repl.start('> ').context = con
当我开始并尝试访问其中一个上下文variables时,会发生这种情况:
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
有效。
我究竟做错了什么?
Object.assign(repl.start('> ').context, con)
当inputrepl.start('> ').context
,该对象非常大。 我猜想分配context
到一个自定义的对象会遗漏一些属性。