简体   繁体   中英

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. 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 .

Looking around at the source code , it appears we can use a sandbox option to set the context. This works:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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