window.hasOwnProperty('innerHeight')在Firefox中返回false

window.hasOwnProperty('innerHeight') returns false in Firefox

本文关键字:Firefox false 返回 innerHeight hasOwnProperty window      更新时间:2023-09-26

在Firefox上运行的单元测试中,我遇到了一个奇怪的问题,在窗口对象上stub出innerHeight属性。

window.hasOwnProperty('innerHeight')返回false(仅在firefox中),这是根据https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty MDN [docs] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)所期望的。然而,定义innerHeight属性的对象是什么?

这失败了(因为没有一个原型)的窗口属性?

window.prototype.hasOwnProperty('innerHeight');

我想知道的主要原因是在测试期间存根属性在chrome中工作,但在firefox中失败,因为firefox报告此属性不属于窗口对象。那么它属于什么物体呢?

prototype属性属于Window构造函数。但我会按照adeneo的建议使用'innerHeight' in window,因为这会检查window对象及其原型链。