typeof、instanceof、hasOwnProperty()、isPrototypeOf()

  1. typeof 操作符
  2. instanceof 操作符
  3. hasOwnProperty()方法
  4. isPrototypeOf()方法

  1.typeof

  用于获取变量的类型,一般只返回以下几个值:string,number,boolean,object,function,undefined.

  typeof作用于引用类型的时候,无论是什么类型的对象,都返回“object”. 

     alert(typeof false);//boolean
        alert(typeof 6);//number
        alert(typeof "hello Jame");
        alert(typeof null);//object
        alert(typeof [1,2,3,4]);//object
        alert(typeof {name:"Jhon",age:23});//object
        alert(typeof function a(){});//function
        alert(typeof new String("str"));//object

  String 与 string的区别:一个是引用类型 object,一个是值类型 string。

     var s = "hello Jame";
        var Str = new String("hello Jhon");
        alert(typeof s);//string
        alert(typeof Str);//object

  凡是通过 new 操作符创建的变量,typeof运算的结果都是object.

     function myFun (){};
        var a = new myFun();
        alert(typeof a);//object
        var b = function myFun1 (){};
        alert(typeof b);//function
        alert(typeof myFun1);//undefined

  2.instanceof

  用来判断一个实例是否属于某种类型。

  更重的一点是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。

     function ClassA(){}
      function ClassB(){}
      function ClassC(){}
      /**
       * 原型继承
       * ClassB继承自ClassA,ClassC继承自ClassB
       */
      ClassB.prototype = new ClassA();
      ClassC.prototype = new ClassB();

     
/**    * 创建ClassB和ClassC的实例    */    var b = new ClassB();    var c = new ClassC();    alert(b instanceof ClassB);//true    alert(b instanceof ClassA);//true    alert(c instanceof ClassA);//true    alert(c instanceof ClassB);//true    alert(c instanceof ClassC);//true

  instanceof复杂用法

    alert(Object instanceof Object);//true
    alert(Function instanceof Function);//true   alert(Function instanceof Object);//true   alert(String instanceof String);//false   alert(Number instanceof Number);//false   alert(Boolean instanceof Boolean);//false

  要从根本上了解instanceof,需从以下两个方面着手:1、语言规范中如何定义该运算符。2、JavaScript的原型继承机制。

  推荐文章:JavaScript instanceof 运算符深入剖析

  3.hasOwnProperty()方法

   用来判断某实例是否具指定的名称属性或方法。是返回true,否则返回false。

   注意:该属性只有是对象本身的一个成员才会返回true。不会检查原型链中是否有给定的方法或属性。

  IE 5.5+、FireFox、Chrome、Safari、Opera等主流浏览器均支持该函数。

      function ClassA(){
          this.fname="";
          this.lname="";
          this.rank = 3;
          this.sayHello = function(){
              alert("Hello:"+fname+lname);
          };
      }
    
      var myPro = {
          name:"张三",
          age:23
      };
      ClassA.prototype = myPro;
      var a = new ClassA();
      alert(a.hasOwnProperty("fname"));//true
      alert(a.hasOwnProperty("lname"));//true
      alert(a.hasOwnProperty("sayHello"));//true
      alert(a.hasOwnProperty("name"));//false

  4.isPrototypeOf()方法

  判断某对象是否是指定实例的原型。是返回true,否则返回false。

  注意:该方法不会检查原型链中的对象。即:只检查本实例的直接原型,不会检查本实例的原型的原型。

      function ClassA(){}
      function ClassB(){}
      function ClassC(){}
      /**
       * ClassB的原型设为ClassA,ClassC的原型设为ClassB
       * 注意:这与原型继承不同
       */
      ClassB.prototype = ClassA;
      ClassC.prototype = ClassB;
      var b = new ClassB();
      var c = new ClassC();
      alert(ClassA.isPrototypeOf(b));//true
      alert(ClassB.isPrototypeOf(c));//true
      alert(ClassA.isPrototypeOf(c));//false

  prototype和_proto_相连的两部分,用isPrototypeOf(),结果返回true.请结合原型链图体会下列代码的结果。

     alert(Object.isPrototypeOf(Function));//false
        alert(Object.prototype.isPrototypeOf(Function.prototype));//true
        
        function ClassD(){};
        alert(Object.prototype.isPrototypeOf(ClassD.prototype));//true
        alert(Function.prototype.isPrototypeOf(ClassD));//true
        
        var d = new ClassD();
        alert(Object.prototype.isPrototypeOf(d.prototype));//false
        alert(Object.isPrototypeOf(d));//false
        
        var obb = new Object();
        alert(Object.prototype.isPrototypeOf(Object));//true
        alert(Object.prototype.isPrototypeOf(obb));//true

 

                                          

 

  引用:JavaScript instanceof 运算符深入剖析

  

转载于:https://www.cnblogs.com/snowcan/p/6435699.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值