zoukankan      html  css  js  c++  java
  • typeof instanceof 和 obj.constructor

      1.typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果:

    'number','boolean','string','function'(函数),'object'(NULL,数组,对象),'undefined' 都是字符串;

    如:

    /*
    typeof方法返回一个字符串,来表示数据的类型
     */
    
    console.log(typeof 'ssss'); // 'string' ---> 这返回的是 字符串
    console.log(typeof 123); // 'number' ---> 这返回的是 字符串
    console.log(typeof true); // 'boolean' ---> 这返回的是 字符串
    console.log(typeof a); // 'undefined' ---> 这返回的是 字符串
    
    function A(){
        this.x = 1;
        this.y = 'abc';
    }
    console.log(typeof A); // 'function' ---> 这返回的是 字符串
    
    console.log(typeof [1,2,3]); // 'object' ---> 这返回的是 字符串
    console.log(typeof {a:1, b:2}); // 'object' ---> 这返回的是 字符串
    
    console.log( typeof window); // 'undefined' ---> 这返回的是 字符串

     我们可以使用typeof来获取一个变量是否存在,如if(typeof a!="undefined"){},而不要去使用if(a)因为如果a不存在(未声明)则会出错;

     

    正因为typeof遇到null,数组,对象时都会返回object类型,所以当我们要判断一个对象是否是数组时

    或者判断某个变量是否是某个对象的实例则要选择使用另一个关键语法instanceof

     

    2.instanceof用于判断一个变量是否某个对象的实例

    /*
    instanceof运算符可以用来判断某个构造函数的prototype属性是否存在于另外一个要检测对象的原型链上。
    
    也就是 instanceof用于判断一个变量是否某个对象的实例
     */
    console.log('a' instanceof Object); // false
    console.log('a' instanceof String); // false
    console.log(1 instanceof Number); // false
    console.log({a:1, b:2} instanceof Object); // true
    console.log([1,2,3] instanceof Array); // true
    console.log([1,2,3] instanceof Object); // true
    
    function A(){
        this.x = 1;
        this.y = 'abc';
    }
    console.log(A instanceof Object); // true
    console.log(A instanceof Function); // true
    var a = new A();
    console.log(a instanceof A);  // true
    console.log(a instanceof Object); // true
    
    console.log(window instanceof Object); // ReferenceError: window is not defined 
    // ReferenceError: window is not defined  
    // 这里的 instanceof 测试的 object 是指 js 语法中的 object,不是指 dom 模型对象。

    3.对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数;

    返回类型:Array、Boolean、Date、Function、Number、Object、String

    /*
    对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数;
    
    语法:
    object.constructor
    
    返回值:
    对象的constructor属性返回创建该对象的函数的引用
     */
    console.log([1,2,3].constructor); // [Function: Array]
    console.log({a:1, b:2}.constructor); // [Function: Object]
    
    function A(){
        this.x = 1;
        this.y = 'abc';
    }
    console.log(A.constructor); // [Function: Function]
    
    var a = 1;
    // console.log(1.constructor); // SyntaxError: Invalid or unexpected token
    console.log(a.constructor); // [Function: Number]
    
    var b = 'abc';
    console.log(b.constructor); // [Function: String]
    console.log('abc'.constructor); // [Function: String]
    
    var c = true;
    console.log(true.constructor); // [Function: Boolean]
    console.log(c.constructor); // [Function: Boolean]
    
    var d = null;
    // console.log(d.constructor); // TypeError: Cannot read property 'constructor' of null
    // console.log(null.constructor); // TypeError: Cannot read property 'constructor' of null
    
    var e = '';
    console.log(e.constructor); // [Function: String]
    console.log(''.constructor); // [Function: String]

    那么接下来,我就有个想法拉,如何判断一个对象的类型到底是 Array 还 Object 呢?

    Method 1: obj.constructor 的方式

      console.log([1,2,3].constructor); // [Function: Array]
      console.log({a:1, b:2}.constructor); // [Function: Object]

    Method 2: typeof 和 instanceof 相结合的方式

    // 形式一
    var a = new Array(1,2,3);
    if(a && typeof a === 'object'){ // 数组 / null / 对象
        if( a instanceof Array ){
            console.log('a 是 Array');
        }
        else {
            console.log('a 是 Object');
        }
    }
    
    // 形式二
    var a = new Array(1,2,3);
    if(typeof a === 'object'){ // 数组 / null / 对象
        if( a instanceof Array ){
            console.log('a 是 Array');
        }
        else if( a instanceof Object ){
            console.log('a 是 Object');
        }
        else {
             console.log('a 是 null');
        }
    }
  • 相关阅读:
    changing a pointer rather than erasing memory cells
    验证码识别 edge enhancement 轮廓增强 region finding 区域查找
    Manipulating Data Structures
    passing parameters by value is inefficient when the parameters represent large blocks of data
    Aliasing 走样
    Artificial Intelligence Research Methodologies 人工智能研究方法
    Thread safety
    include pointers as a primitive data type
    flat file
    functional cohesion
  • 原文地址:https://www.cnblogs.com/cnblogs-jcy/p/8577702.html
Copyright © 2011-2022 走看看