gpt4 book ai didi

新 String ("a") 对象的 JavaScript(严格)相等

转载 作者:行者123 更新时间:2023-12-04 01:46:15 26 4
gpt4 key购买 nike

有在ECMAScript Language Specification11.9.1 等于运算符 (==):

NOTE 3 The equality operator is not always transitive. For example, there might be two distinct String objects, each representing the same String value; each String object would be considered equal to the String value by the == operator, but the two String objects would not be equal to each other. For Example:

  • new String("a") == "a" and "a" == new String("a") are both true.
  • new String("a") == new String("a") is false.

但在11.9.6 严格相等比较算法中:

7. Return true if x and y refer to the same object. Otherwise, return false.

但是没有,用jsc

new String("a")===new String("a")

是真的。

那是 jsc.exe 错误吗?或者它们是同一个对象?为什么?

最佳答案

js中的运算符===如果类型相等且值相等则返回true。所以它返回 true 是正确的,因为它们都是 String 对象并且它们的值是“a”。

查看文档 http://www.w3schools.com/js/js_comparisons.asp

关于新 String ("a") 对象的 JavaScript(严格)相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23847475/

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