Eval() = Unexpected token : error

前端 未结 8 1788
囚心锁ツ
囚心锁ツ 2020-12-09 04:28

I tried this simple JavaScript code:

eval(\'{\"Topics\":[\"toto\",\"tata\",\"titi\"]}\')

In the Chrome console, for example, this returns

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 05:00

    Because eval does not force an expression context and the string provided is an invalid JavaScript program, thus the first three tokens (and how they are looked at) are:

    {            // <-- beginning of a block, and NOT an Object literal
    "Topics"     // <-- string value, okay (note this is NOT a label)
    :            // <-- huh? expecting ";" or "}" or an operator, etc.
    

    Happy coding.

提交回复
热议问题