gpt4 book ai didi

javascript - 无法列出 window.document 属性 - 为什么?

转载 作者:行者123 更新时间:2023-11-29 10:59:01 25 4
gpt4 key购买 nike

在 Firefox 60.2 中运行此 JavaScript 仅列出一个属性(“位置”),但还有许多其他属性,如“document.title”等。

window.console.log("List props: " + Object.keys(window.document).sort().join(' / '));

为什么会这样?安全?这在技术上是如何做到的?如何列出所有属性?

最佳答案

Object.keys(o) 返回 o自己的、可枚举的属性。

  • Own:直接在 o 上定义的属性,而不是在其 prototype chain 上定义的属性:
  • 可枚举:控制在列出对象属性时是否包含给定属性的标志。

在这种情况下,您期望的大多数键都在 documentprototype chain 中的另一个对象上定义。 :

document.hasOwnProperty("title"); // false
document.__proto__.__proto__.hasOwnProperty("title"); // true
document.__proto__.__proto__.propertyIsEnumerable("title"); // true

您可以使用 for...in 循环来查找对象或其原型(prototype)链上定义的可枚举属性:

for (let key in window.document) {
console.log(key);
}

关于javascript - 无法列出 window.document 属性 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50947516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com