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

标签 javascript comparison

有在ECMAScript Language Specification 11.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/

相关文章:

javascript - 使用 vue.js,如何将事件标签设置为当前事件路径?

javascript - 我们如何使用 jquery 更改 tr 的背景颜色?

javascript - 数组 .includes() 给出意外行为

c - C语言中IF和SWITCH/CASE的区别

python - 链接 "is"运算符

c++ - 比较运算符的行为是什么?

javascript - 如何检查它是字符串还是json

javascript - Webpack 和 Javascript 变量范围,在所有模块中使用 var = 可以吗?

matlab - matlab中不同长度的字符串/数组的比较和移位

php - 检查帖子是否已发布 15 分钟