构造函数中的常用方法

一.    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                            判断是否具有对应属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值