javascript - Node VM - Return an object implicitly -
this question has answer here:
i looking way simple scripting in node js , discovered vm module. documentation specifies run* methods return result of execution, thought "hey, why not return object way , call on properties in main script?"
so fire node repl:
$ node > var vm = require('vm'); undefined > vm.runinnewcontext("{ foo: 'bar' }") 'bar' > vm.runinnewcontext("{ foo: 'bar', baz: 'qux' }") evalmachine.<anonymous>:1 { foo: 'bar', baz: 'qux' } ^ syntaxerror: unexpected token : >
not quite expected results. interestingly, if return result of assignment...
> vm.runinnewcontext("exports = { foo: 'bar', baz: 'qux' }") { foo: 'bar', baz: 'qux' }
can explain behavior me?
v8 interpreting braces code block containing labels. wrap in parentheses: vm.runinnewcontext("({foo: 'bar', baz: 'qux'})")
.
Comments
Post a Comment