简体   繁体   English

为什么“{} || {}“抛出语法错误?

[英]Why “{} || {}” throws syntax error?

This works: 这有效:

var a = {} || {};

While this does not: 虽然这不是:

{} || {};
Uncaught SyntaxError: Unexpected token ||
    at Object.InjectedScript._evaluateOn (<anonymous>:905:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:838:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)

Why is that? 这是为什么? Tested on ff and chrome. 在ff和chrome上测试过。

Javascript considers braces to be a code block. Javascript认为大括号是一个代码块。 Try this: 尝试这个:

({} || {});

Because in {} || {}; 因为在{} || {}; {} || {}; , the first {} is not interpreted as an [empty] object. ,第一个{} 不被解释为[空]对象。 It's interpreted as a block statement . 它被解释为块语句

Try this: 尝试这个:

({}) || {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM