简体   繁体   English

未定义变量,用于评估coffeescript字符串

[英]variable undefined evaluating a coffeescript string

The following works from the coffeescript REPL but it doesn't work if I put it in a file and run it. 以下内容来自coffeescript REPL,但如果我将其放入文件中并运行,它将无法正常工作。 It says that "a is not defined". 它说“未定义”。

Coffee = require 'coffee-script'
a = {b: 1}
console.log Coffee.eval('a.b')

Any ideas how to get this to work as a script? 任何想法如何使它作为脚本工作? I couldn't find any documentation on .eval . 我在.eval上找不到任何文档。

Looking around at the source code , it appears we can use a sandbox option to set the context. 看看源代码 ,看来我们可以使用sandbox选项来设置上下文了。 This works: 这有效:

Coffee = require 'coffee-script'
a = {b: 1}
console.log Coffee.eval('a.b', {sandbox:{a}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM