JavaScript:{} == false是一个SyntaxError? [英] JavaScript: {}==false is a SyntaxError?

查看:95
本文介绍了JavaScript:{} == false是一个SyntaxError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firefox 3.5中,我在Firebug控制台中输入:

In Firefox 3.5, I type this in the Firebug console :

false=={} // => evals to false
{}==false // syntax error

有什么解释这个?

推荐答案

{

在声明开头发出声明块信号(参见 ECMA-262-3 第12.1节),其中包含一系列陈述。

at the start of a statement signals a ‘statement block’ (see ECMA-262-3 section 12.1), which contains a list of statements.

}

立即结束语句块,其中没有语句。没关系。但现在解析器正在寻找下一个声明:

immediately ends the statement block with no statements in it. That's fine. But now the parser is looking for the next statement:

==false

嗯?那不是陈述;语法错误。

Huh? That's not a statement; syntax error.

什么是语句块?好吧,每次你说的时候,你都在写一个声明块:

What are statement blocks for? Well, you are writing a statement block every time you say:

if (something) {
    ...
}

JavaScript将这些流控制语句定义为:

JavaScript defines these flow-control statements as:

if "(" <expression> ")" <statement> [else <statement>]

ie。在单一的声明形式,没有大括号。然后,它允许您在任何可以使用单个语句的地方使用语句块,这意味着您可以使用if-braces-many-statements。但它也意味着你可以自己拥有一个语句块而没有相关的流控制语句。

ie. in the single statement form with no braces. It then allows you to use a statement-block anywhere you can use a single statement, which means you can have if-braces-many-statements. But it also means you can have a statement-block on its own with no associated flow-control statement.

这绝对没有实际意义!你可能会认为它给你信息隐藏,但没有:

This serves absolutely no practical purpose! You might be tempted to think it gave you information-hiding, but no:

var a= 1;
{
    var a= 2;
}
alert(a);

...结果 2 ,因为语句块本身不会创建新范围。

...results in 2, because statement blocks don't in themselves create a new scope.

JavaScript以这种方式定义流控制和语句块,因为C(以及从中派生的其他语言)可以。这些语言没有使 {} 服务双重作为Object文字表达式,所以他们没有使这另一个JS错误的歧义。

JavaScript defines flow control and statement blocks in this manner because C (and other languages derived from it) did. Those languages didn't make {} serve double-duty as an Object literal expression though, so they didn't have the ambiguity that makes this another JS misfeature.

即使这个想法 - 文字:

Even this wannabe-literal:

{
   a: 1
}

是一个有效的语句块,因为':'用于表示标签一份声明。 (和 1 是一个无用的表达式语句,省略了分号。)标签是从C继承的另一个很少在JavaScript中使用的功能。它们并不像块一样毫无意义,但它们很少需要并且通常被认为是味道不佳。

is a valid statement block, because ‘:’ is used to denote a label in a statement. (and 1 is a useless expression-statement, with the semicolon omitted.) Labels are another feature inherited from C that are rarely used in JavaScript. They're not totally pointless like the blocks, but they're seldom needed and often considered in poor taste.

(具有两个属性的文字将导致语法错误,因为对象文字使用逗号分隔符,但标记的语句必须用分号分隔。)

(A literal with two properties will cause a syntax error, as object literals use comma separators, but labelled statements must be separated by semicolons.)

这是不是唯一的地方,JavaScript的松散语法可以通过对你认为是表达式的某些其他声明来绊倒你。

This is not the only place where JavaScript's loose syntax can trip you up by making some other statement of something you think is an expression.

这篇关于JavaScript:{} == false是一个SyntaxError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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