作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如我有一个数组,我们称它为 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/
我是一名优秀的程序员,十分优秀!