JavaScript eval(“{}”)返回行为? [英] JavaScript eval("{}") return behavior?

查看:171
本文介绍了JavaScript eval(“{}”)返回行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 ECMA-262规范,以下语句返回 1

eval("1;;;;;")
eval("1;{}")
eval("1;var a;")

确保:


StatementList的值是最后生成的值的值StatementList中的陈述。

The value of a StatementList is the value of the last value producing Statement in the StatementList.

你能解释这些不同的回报吗?

Can you explain these different returns ?

eval("{}") // undefined
eval("var a={}; a;") // {}
eval("var a={};") // undefined

1之间的区别是什么? {};

推荐答案

单独留下, {} 被解释为,而不是对象。它不包含任何语句,因此不会影响 eval(1; {})的值。要强制它被解释为对象,您可以使用括号:

Left alone, {} is interpreted as a block, not an object. It contains no statements, so does not affect the value of, say, eval("1;{}"). To force it to be interpreted as an object, you can use parentheses:

eval("1;({})"); // {}

这篇关于JavaScript eval(“{}”)返回行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆