jQuery uses (new Function(“return ” + data))(); instead of eval(data); to parse JSON, why?

后端 未结 3 884
后悔当初
后悔当初 2020-12-03 10:43

This link shows you that jQuery uses (new Function(\"return \" + data))(); for older browsers, to parse a JSON string instead of eval().

Wh

3条回答
  •  遥遥无期
    2020-12-03 11:37

    As to why jQuery specifically uses new Function(), John Resig answered this on the jQuery forums

    Using eval causes all sorts of problems for code minifiers since it's not clear what could be executing in the eval. Looking at the last results from that run it looks like new Function is fairly equivalent to eval and even slightly faster sometimes. The one exception was Safari 4 - but those results are dated, Safari 4 shipped with a native JSON.parse implementation, which we use.

提交回复
热议问题