gpt4 book ai didi

javascript - 是否有一个等于 hasOwnProperty() 的方法可以遍历原型(prototype)链?

转载 作者:行者123 更新时间:2023-11-30 08:41:58 25 4
gpt4 key购买 nike

例如我有一个数组,我们称它为 myArray 其中:

 var myArray = ['foo', 'bar']; 

尽管 myArray.join() 将返回 'foo,bar' ,检查 myArray.hasOwnProperty('join') , 将返回 false ,因为简单的 hasOwnProperty() 不会遍历原型(prototype)链。

有没有办法通过遍历原型(prototype)链来完成相同的功能?

P.S:即使是自定义方法也可以。

最佳答案

您可以使用 in 运算符。

The in operator returns true for properties in the prototype chain.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

喜欢:

if ('join' in myArray) {
...
}

关于javascript - 是否有一个等于 hasOwnProperty() 的方法可以遍历原型(prototype)链?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25493715/

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