gpt4 book ai didi

javascript - Object.keys(anObject) 是否返回一个对象的原型(prototype)?

转载 作者:数据小太阳 更新时间:2023-10-29 04:31:43 25 4
gpt4 key购买 nike

<分区>

我正在阅读 Eloquent JavaScript's Map section我无法理解它的最后一段:

If you do have a plain object that you need to treat as a map for some reason, it is useful to know that Object.keys returns only an object’s own keys, not those in the prototype. As an alternative to the in operator, you can use the hasOwnProperty method, which ignores the object’s prototype.

然后我假设 Object.keys 不返回对象从其原型(prototype)继承中获得的属性和方法。所以我尝试了以下方法:

var anObject = {};
console.log(Object.keys(anObject)); //Array []
console.log("toString" in Object.keys(anObject)); //true
console.log(anObject.hasOwnProperty("toString")); //false

显然,toStringObject.keys(anObject) 返回的数组中,但是当我记录它的键时返回一个空数组?我是不是理解错了这段话?

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