javascript - [1] == [1] Ist das falsch oder wahr?
伊谢尔伦
伊谢尔伦 2017-05-19 10:44:27
0
6
1946

[1] == [1] Die Anzahl der gedruckten Werte ist falsch. Weiß jemand warum?

伊谢尔伦
伊谢尔伦

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

Antworte allen(6)
过去多啦不再A梦

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

相等(==)

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

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

刘奇

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

阿神

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

洪涛

当比较的值是引用值时,会比较两个值在内存中是否为同一对象,这里的[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

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!