一. instanceof 检测变量类型的方法
function Student(name,age){
this.name=name
this.age=age
}
Student.prototype.showInfo=function(){
console.log(this.name+this.age)
}
var s1=new Student("Wangyi",12)
console.log(s1 instanceof Student)
二. isPrototypeOf() 判断对应对象是否是基于对应构造函数创建的
function Student(name,age){
this.name=name
this.age=age
}
Student.prototype.showInfo=function(){
console.log(this.name+this.age)
}
var r1=Student.prototype.isPrototypeOf(s1) // == s1 instanceof Student
var r2=Animal.prototype.isPrototypeOf(s1)
console.log(r1)
console.log(r2)
三. 对象.hasOwnProperty(属性名 key) 判断当前属性名是否在其构造函数里
function Student(name,age){
this.name=name
this.age=age
}
Student.prototype.show=function(){
console.log(123)
}
var s1=new Student("玩意",21)
var r1=s1.hasOwnProperty("name")
var r2=s1.hasOwnProperty("count")
console.log(r1)
console.log(r2)
四. in 判断对象是否具备对应属性
function Student(name,age){
this.name=name
this.age=age
}
Student.prototype.show=function(){
console.log(123)
}
var s1=new Student("玩意",21)
var r1= "name" in s1
var r2= "show" in s1
console.log(r1)
console.log(r2)
hasOwnProperty 判断是否在构造函数里
in 判断是否具有对应属性