【问题标题】:JavaScript: Why is the output false? [duplicate]JavaScript:为什么输出为假? [复制]
【发布时间】:2021-11-21 16:10:31
【问题描述】:

// These are true

console.log(window === this)
console.log(window === self)
console.log(this === self)

console.log(window === this === self) // Why is this false?

window 等于this
window 等于self
this 等于self

那么,为什么 window === self === this 在 JavaScript 中为假

【问题讨论】:

  • 因为a === b === c 等价于(a === b) === c,因此true === c 如果c 不是布尔值则显然是错误的

标签: javascript object output equality


【解决方案1】:

因为true 不等于this

这个表达式:

window === self === this

相当于:

true === this

这是错误的,因为this 不是布尔值。

【讨论】:

    【解决方案2】:

    因为您不能在 javascript 中以这种方式链接相等运算符。 window === this === self 实际上与:(window === this) === self 相同,其中首先计算括号。换句话说,window === this === self 等于 true === self,其计算结果为 false

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-13
      • 2023-03-10
      • 2014-07-24
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      相关资源
      最近更新 更多