javascript - [1] == [1] 这个是false 还是 true
伊谢尔伦
伊谢尔伦 2017-05-19 10:44:27
[JavaScript讨论组]

[1] == [1] 打印出的值数false ,有谁知道为什么吗

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(6)
过去多啦不再A梦

比较操作符中的相等运算符,有详细的介绍:

相等(==)

比较操作符会为两个不同类型的操作数转换类型,然后进行严格比较。当两个操作数都是对象时,JavaScript会比较其内部引用,当且仅当他们的引用指向内存中的相同对象(区域)时才相等,即他们在栈内存中的引用地址相同。

楼上说的都对,因为[1] == [1]中,两个数组是不同的对象,所以不相等。

怪我咯

JavaScript中,数组是Object
这一语句通过字面量创建了两个Array,他们是不一样的对象,因此不相等。

阿神

2个Array对象不是同一个对象。

PHP中文网

当比较的值是引用值时,会比较两个值在内存中是否为同一对象,这里的[1]和[1]不同,所以是false

習慣沉默

Ecma-262.pdf
7.2.13 Abstract Equality Comparison
已经明确说明了,这里的 [1] = [1] 实际是 [1] === [1] 正好试用第一条。


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 the same as Type(y), then a. Return the result of performing Strict Equality Comparison x === y.

  2. If x is null and y is undefined, return true.

  3. If x is undefined and y is null, return true.

  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x ==
    ToPrimitive(y).

  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x)
    == y.

  10. Return false.

伊谢尔伦

对象类型会比较内存地址,地址不同所以为false

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号