判断某个属性是否在自己上面,而不是在原型上(hasOwnProperty)
var obj = {
name : 'why',
age : 18
}
var info = Object.create(obj,{
address:{
value:'北京市',
enumerable:true
}
})
console.log(info);
console.log(info.__proto__);{ name: 'why', age: 18 }
console.log(info.hasOwnProperty('address'));
console.log(info.hasOwnProperty('age'));
不管是不是在原型上,只要存在就是true(in操作符)
var obj = {
name : 'why',
age : 18
}
var info = Object.create(obj,{
address:{
value:'北京市',
enumerable:true
}
})
console.log("address" in info);
console.log("name" in info);
检测构造函数的prototype是否出现在某一对象的原型上(instanceof)
function inheritPrototype(SubType,SuperType){
SubType.prototype = Object.create(SuperType.prototype)
Object.defineProperty(SubType.prototype,"constructor",{
enumerable:false,
configurable:true,
writable:true,
value:SubType
})
}
function Person(){
}
function Student(){
}
inheritPrototype(Student,Person)
var stu = new Student()
console.log(stu instanceof Student);
console.log(stu instanceof Person);
console.log(stu instanceof Object);