在 javascript 中,为什么 {}!==Object()? [英] IN javascript,why {}!==Object()?
问题描述
给定
var o = {};
var p = new Object();
p === o; //false
o.__proto__===p.__proto__ // true
为什么这是假的?
请告诉我返回false的直接原因??
please tell me the immediate reason to return false??
推荐答案
===
对象 定义为:
11.9.6 严格相等比较算法
11.9.6 The Strict Equality Comparison Algorithm
比较 x === y
,其中 x
和 y
是值,产生 true
或假
.这样的比较执行如下:
The comparison x === y
, where x
and y
are values, produces true
or
false
. Such a comparison is performed as follows:
...
7.如果 x
和 y
引用同一个对象,则返回 true
.否则,返回假
.
7. Return true
if x
and y
refer to the same object. Otherwise, return
false
.
在这种情况下,虽然两者都是空对象,但它们是单独创建的,因此不会引用同一个对象.
In this case, although both are empty objects, they are created separately and hence do not refer to the same object.
作为旁注,两种结构都做同样的事情;但通常的做法是使用 {}
.
As a side note, both constructions do the same thing; but it is common practice to use {}
.
这篇关于在 javascript 中,为什么 {}!==Object()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!