[js中的hasOwnProperty和isPrototypeOf方法使用实例]
function Parent() {
this.name = "Parent";
}
Parent.prototype.alertP = function(){
alert('Parent-alertP');
};
function Child(){
this.age = 23;
}
Child.prototype.alertC = function(){
alert('Child-alertC');
};
function Other(){}
Other.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
var child = new Child();
console.log(child);
// instanceof:判断该对象是否是另一个对象的实例
console.log(child instanceof Parent); // true
console.log(child instanceof Child); // true
// isPrototypeOf: 判断一个对象是否是一个实例的原型
console.log(Parent.isPrototypeOf(child)); // false
console.log(Parent.prototype.isPrototypeOf(child)); // true
console.log(Other.isPrototypeOf(child)); // false
console.log(Other.prototype.isPrototypeOf(child)); // true
console.log(Child.isPrototypeOf(child)); // false
console.log(Child.prototype.isPrototypeOf(child)); // true
// hasOwnProperty: 判断对象是否有某个特定的属性
// prototype 是函数才有的属性,__proto__ 是每个对象都有的属性(不是所有浏览器都有)
console.log(child.__proto__); // Parent{constructor:function}
console.log(child.prototype); // undefined
console.log(child.constructor); // function Child() {this.age = 23; }
console.log(Child.hasOwnProperty("name")); // true
console.log(Parent.hasOwnProperty("name")); // true
console.log(child.hasOwnProperty("age")); // true
console.log(Child.hasOwnProperty("age")); // false
console.log(Parent.hasOwnProperty("age")); // false
console.log(Child.hasOwnProperty("alertP")); // false
console.log(Child.__proto__); // function() {[ native code ]}
console.log(Child.prototype); // Parent{constructor:function}