hasOwnProperty() 所有继承了
1 2 3 4 5 | in操作符:检测指定对象(右边)原型链上是否有对应的属性值。 hasOwnProperty方法:检测指定对象自身上是否有对应的属性值。 两者的区别在于in会查找原型链,而hasOwnProperty不会 |
hasOwnProperty()语法
1 | obj.hasOwnProperty(prop) |
参数:prop要检测的属性的
返回值:用来判断某个对象是否含有指定的属性的布尔值
示例
1 2 3 4 5 6 7 | Object.prototype.bar = {bar:'object prototype'}; var foo = { goo: undefined }; console.log('bar' in foo);// expected output: true(in会查找原型链) console.log(foo.hasOwnProperty('bar'));// expected output: false console.log(foo.hasOwnProperty('goo'));// expected output: true |