简体   繁体   English

为什么此表达式会导致“ReferenceError”?

[英]Why does this expression result in `ReferenceError`?

Why does incrementing the same thing behave differently when I reference it via a variable?为什么当我通过变量引用它时递增相同的东西会有不同的行为?

function f() {
    return {};
}

let x = {};
x++; // OK
(f())++ // ReferenceError

The difference is due to that when js is evaluating the expression it starts by checking if the left hand side is convertible to a number and by quirkyness an object is converted to Nan whereas a function is is not and js throws an exception when trying to convert it before it is evaluated.不同之处在于,当 js 评估表达式时,它首先检查左侧是否可转换为数字,并且由于古怪,object 被转换为 Nan,而 function 则不是,并且 js 在尝试转换时抛出异常它在被评估之前。 See also here: https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-postfix-increment-operator另见此处: https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-postfix-increment-operator

This whole code doesn't make any sense.整个代码没有任何意义。 You cannot increment a object.您不能递增 object。

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

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