How Does: "(a = b) != c" work in Javascript?

How Does: "(a = b) != c" work in Javascript?

本文关键字:quot work in Javascript Does How      更新时间:2023-09-26

我正在查看jQuery代码$.extend()

我发现了这个:

    if ( (options = arguments[ i ]) != null ) {
        // Extend the base object

我想知道如果我们把options = arguments[i]移出括号会发生什么?

a = bb赋值给a,返回b。因此,(a = b) != c将把b赋值给a,然后检查b != c。括号是,否则,!=将在=之前求值,因为运算符优先级(比较在赋值- source之前求值)

 a = b!= c 

的作用如下:

 a = (b != c)

因为=运算符在所有运算符中优先级最低。

 b = 5 
 c = 10 
 a = b != c
>>> false
>>> a will have false value here

这是优先级图和演示

对于(options = arguments[ i ]) != null, options将被赋值为argument[ i ],然后与null进行比较。

赋值的优先级低于相等,这意味着

arguments[i] != null

将在

之前处理
options = arguments[i]

给出与期望结果不同的结果。为了更好地理解它,请查看这里并阅读操作符优先级