typeOf 和 instanceof 基础数据类型和引用数据类型

69 阅读1分钟

image.png

    let a = null;
    let b = undefined;
    let c = true;
    let d = 1;
    let e = '1';
    let f = Symbol;

    console.log('typeOf(a) :>> ', typeof(a)); object
    console.log('typeof(b) :>> ', typeof(b)); undefined
    console.log('typeof(c) :>> ', typeof(c)); boolean
    console.log('typeof(d) :>> ', typeof(d)); number
    console.log('typeof(e) :>> ', typeof(e)); string
    console.log('typeof(f) :>> ', typeof(f)); function

    let a1 = function(){};
    let b1 = {};
    let c1 = ['1','2'];
    
    console.log('typeof(a1) :>> ', typeof(a1)); function
    console.log('typeof(b1) :>> ', typeof(b1)); object
    console.log('typeof(c1) :>> ', typeof(c1)); object
    
    console.log('所有基础类型 原始值都不是对象 返false :>> ', a instanceof Object);false
			
    console.log('a1 instanceof object :>> ', a1 instanceof Array); false
    console.log('b1 instanceof object :>> ', b1 instanceof Array); false
    console.log('c1 instanceof object :>> ', c1 instanceof Array); true
    
    console.log('想知道它是什么类型的对象 :>> instanceof 区分数组对象',);