【问题标题】:Why performance.hasOwnProperty('getEntries') returns false while typeof performance.getEntries returns function?为什么 performance.hasOwnProperty('getEntries') 返回 false 而 typeof performance.getEntries 返回函数?
【发布时间】:2023-03-28 12:28:02
【问题描述】:

Window 属性性能有一个函数调用 getEntries 来检索所有性能条目。它适用于所有现代浏览器,但不适用于 Safari 10 等一些较旧的浏览器。添加对工作浏览器的检查....

如果我们尝试使用performance.hasOwnProperty('getEntries') 进行验证,它总是返回false。 但如果使用 typeof performance.getEntries === 'function',它就可以工作。

想了解其背后的逻辑。

【问题讨论】:

  • js 做的是:如果你调用performance.getEntries 并且 JS 没有找到它。它沿着原型链向上寻找它。 “上升”我的意思是这里performance.__proto__ 和搜索。如果在那种情况下它是performance.__proto__.hasOwnProperty("getEntries") // true。万一 JS 没有发现它继续上升链 performance.__proto__.__proto__ 等等。链中的最后一项是 javascript 的基础对象。如果您尝试再上 1 个,您将收到 null 所以 null 是链的末端

标签: javascript performance window typeof hasownproperty


【解决方案1】:

performance没有自己的属性“getEntries”,该属性由Performance的prototype所有,它是性能对象的构造函数。

当你使用performance.getEntries()时,实际上是调用了Performance.prototype.getEntries()。

【讨论】:

猜你喜欢
  • 2017-07-16
  • 1970-01-01
  • 2015-06-21
  • 2022-08-14
  • 2020-10-17
  • 2011-02-17
  • 2019-11-11
相关资源
最近更新 更多