只是想理解,后面的行应该返回什么以及如果proto.constructor = Object
怎么办?
return !!( proto === null || proto.constructor === Object );
更具体地说,!!
是如何工作的?
它将返回一个布尔值:true或false。 !!
就像双翻转一样:如果一个值为假,则将其翻转为真,然后将其翻转回假。基本上,有!!
和没有!!
是一样的。
如果proto === null
或proto.constructor === Object
,则为 true。
错误的 iff proto !== null
和 proto.constructor !== Object
.
因此,如果proto.constructor = Object
,它将返回 true。
!
(逻辑 NOT)运算符
如果其单个操作数可以转换为 true,则返回 false;否则,返回 true。(MDN)
一个 typeError。 因为 proto 没有定义并且不等于 null,所以 js 会尝试从不存在的 proto 中获取属性"constructor"。