为什么 Object.getOwnPropertyDescriptors() 在我将历史对象(它是 BOM 的一部分)传递给它时不起作用?

Why Object.getOwnPropertyDescriptors() does not work when I pass the history object (which is part of the BOM) to it?

浏览器对象模型 (BOM) 中有不同的对象,例如历史、位置、文档等。我可以获得位置和文档对象的 属性 描述符,但不能获取历史对象的描述符.为什么会这样?

因为历史对象实例没有“自己的”属性。所有这些都是从其原型继承的,根据定义,这些原型不包含在 Object.getOwn[anything] 函数中。当你 运行 它时,你还会注意到 Object.getOwnPropertyNames(history) 是一个空数组,告诉我们很多。

(不同于location or document, which are objects with defineOwnProperty steps as part of their formal instantiation specification, history is an "empty" object with fairly special handling, but following the normal rules for object instantiation

相反,如果您想查看其“即时”API,请使用 Object.getOwnPropertyDescriptors(Object.getPrototypeOf(history))