gpt4 book ai didi

javascript - ECMA-262 ===算法解释

转载 作者:行者123 更新时间:2023-11-28 11:27:06 24 4
gpt4 key购买 nike

我试图理解 JavaScript 中 === 运算符的确切算法。它被定义为类似

The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is Number, then
    • If x is NaN, return false.
    • If y is NaN, return false.
    • If x is the same Number value as y, return true.
    • If x is +0 and y is −0, return true.
    • If x is −0 and y is +0, return true.
    • Return false.
  5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. Return true if x and y refer to the same object. Otherwise, return false.

现在如果我写一些类似的东西

var t1 = undefined,t2 = 2;
typeof(t1); //"undefined"
typeof(t2); //"number"

t1 === t2; //returns false ?????

考虑第 2 点和第 3 点:它应该返回 true。我正在 Chrome 15.0.874.106 m 中测试它。有人可以解释一下这种情况到底发生了什么吗?

最佳答案

你必须按顺序进行,如果 Type(x) 与 Type(y) 不同,则返回 false。。由于 false 已返回,因此永远不会到达点 2 或 3。

关于javascript - ECMA-262 ===算法解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8012903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com