Javascript hasOwnProperty 方法 & in 关键字

(编辑:jimmy 日期: 2024/10/9 浏览:2)

此方法无法检查该对象的原型链中是否具有该属性;该属性必须是对象本身的一个成员。
in 操作检查对象中是否有名为 property 的属性。也可以检查对象的原型,判断该属性是否为原型链的一部分。
复制代码 代码如下:
function Test(){
this. a= 'abc';
}
Test.prototype.b='efg';

var test=new Test;
alert(test.hasOwnProperty('a'));//输出 true
alert(test.hasOwnProperty('b'));//输出 false

alert('a' in test);//输出 true
alert('b' in test);//输出 true