javascript eval and object evaluation

后端 未结 1 425
忘掉有多难
忘掉有多难 2020-12-10 06:03

I have a part of a debugging framework that needs to be able to run time eval objects.

Specifically, if I have a string like this \"{a: 1, b:2}\" it nee

1条回答
  •  爱一瞬间的悲伤
    2020-12-10 06:24

    You can do it using () to have it parse it as an object, rather than a statement, like this:

    eval("(" + str + ")");
    

    Though, you should use JSON.parse() first, if the browser supports it.

    0 讨论(0)
提交回复
热议问题