gpt4 book ai didi

javascript - 为什么 jQuery 对象有一个属性,但 hasOwnProperty 说它没有?

转载 作者:行者123 更新时间:2023-11-29 16:38:17 26 4
gpt4 key购买 nike

enter image description here

enter image description here

enter image description here

我可以读取 className 属性,但 hasOwnProperty 返回 false。有件事我错过了。

最佳答案

您正在检查的对象不是 jQuery 对象:它是 DOM 节点。因此,它从树的更上方继承了 className 属性(我相信是从 Element 继承的)。由于 className 是继承的,hasOwnProperty 返回 false,因为 hasOwnProperty 不遍历原型(prototype)链。

来自问题“有没有办法检查对象是否具有继承的属性?”下面:继承的属性,如上面的 className would appear in that object's prototype chain

在问题中的 DOM 节点示例中:

$(".comment")[0].hasOwnProperty('className'); // -> false because className comes from the prototype chain.

'className' in $(".comment")[0].__proto__; // -> true: className is inherited from up the chain

关于javascript - 为什么 jQuery 对象有一个属性,但 hasOwnProperty 说它没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49277888/

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