gpt4 book ai didi

javascript - 使用 Object.keys() 打印属性

转载 作者:行者123 更新时间:2023-11-28 15:09:10 24 4
gpt4 key购买 nike

我正在尝试使用 Object.keys() 方法打印 Object's 自己的属性(不包括 prototype 属性)。

var obj = {
name : "Jane"
}

keys = Object.keys(obj); //["name"]

当我这样做

console.log("length" in keys) //It is printing true?

我相信“length”来自原型(prototype)。为什么它存在于keys数组中?

最佳答案

Why does it exist in keys array?

它确实存在于keys array中,因为keysArray的一个实例,并且Array具有属性length .

这相当于说

if ( "length" in keys)
{
console.log(true)
}

检查in operator文档也在这里。

The in operator returns true if the specified property is in the specified object.

关于javascript - 使用 Object.keys() 打印属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37405203/

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