在 javascript 中,为什么 {}!==Object()? [英] IN javascript,why {}!==Object()?

查看:57
本文介绍了在 javascript 中,为什么 {}!==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,其中 xy 是值,产生 true.这样的比较执行如下:

The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

...

7.如果 xy 引用同一个对象,则返回 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆