hasOwnProperty,isPrototypeOf, instanceOf
- hasOwnProperty , 检测一个属性是否存在于”实例”中
function Person(name,age) {
this.name = name;
}
var p1 = new Person('herman');
Person.prototype.age = 1;
console.log(p1.hasOwnProperty('name')); //true
console.log(p1.hasOwnProperty('age')); //false
- isPrototypeOf
判断一个对象内部属性__proto__
和对象直接是否存在关系。
如果这个对象的__proto__
指向调用isPrototypeof()方法的对象(Person.prototype
),将返回true.
Person.prototype.isPrototypeOf(p1); //true
Person.prototype.isPrototypeOf(p2); //false
例子中用原型对象(Person.prototype)的isPrototypeOf()方法测试p1和p2。结果p1有个指向Person.prototype的指针,p2没有这个指针。
- instanceOf 判断前面的对象是否为后边对象的实例
console.log(p1 instanceof Person) //true
var p2 = {}
console.log(p2 instanceof Object) //true
console.log(p2 instanceof Person) //false