有没有办法将 JavaScript 字符串作为某些函数执行?我正在使用eval
,但它不起作用。
我正在尝试这个,因为我需要从 YML 文件中读取值以进行我的 serenity js 测试。
这就是我想要做的
this.data = "element1.element2"
this.test1 = "safeLoad(readFileSync(`config/${this.fileName}.yml`, 'utf8'))";
console.log(this.test1)
console.log(`${this.test1}.${this.data}`);
这就是我尝试执行字符串的方式
eval(`${this.testabc}.${this.data}`)
但是,当我执行此操作时,出现以下错误:
ReferenceError: safeLoad is not defined
PS:如果我正常执行代码(没有 eval)它工作正常!
safeLoad(readFileSync(`config/${this.fileName}.yml`, 'utf8')).element1.element2
有谁知道如何将这样的代码作为字符串执行?