gpt4 book ai didi

javascript - 将 eval 与 `with` 结合使用

转载 作者:行者123 更新时间:2023-11-30 13:10:34 25 4
gpt4 key购买 nike

我知道通常不鼓励使用 evalwith,但暂时把它放在一边,我无法让这段代码正常工作:

with({ a: 'hello world' }){
var output = eval('{ text: a }')
}

我期望输出对象:

{ text: "hello world" }

但是 outputstring 'hello world'

谁能帮我看看我错过了什么?

最佳答案

你需要把它括起来:

 var output = eval('({ text: a })')

参见例如Why does JavaScript's eval need parentheses to eval JSON data?

关于javascript - 将 eval 与 `with` 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14006016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com