• typeof与instanceof比较+undefined与null各种值的相互比较


    1、typeof:返回一个字符串

    根据typeof判断对象
    表达式                          返回值
    typeof undefined           'undefined'
    typeof true                   'boolean'
    typeof 123                   'number'
    typeof "abc"               'string'
    typeof function() {}       'function'
    
    typeof {}                   'object'
    typeof []                   'object'
    typeof null                   'object'
    
    function f(...args) {
        console.log(typeof args); //object
      
    console.log(args instanceof Array); //true
    }

    总结:typeof返回值是一个字符串,该字符串说明运算数的类型,一般只返回一下六种:

    number、string、Boolean、undefined、function、object(null、对象、数组)

    对于数组、对象以及null,返回值都为object,这正是typeof的局限性。

    2、instanceof:用于判断一个变量是否为某个类的实例

        var a = [];
        console.log(a instanceof Array); //true
        console.log(a instanceof Object); //true
    以上两个结果都为true,因为Array是Object的子类。
    
        function test(){};
        var a = new test();
        console.log(a instanceof test) // true

    3、null与undefined比较

    以下结果涉及到以下类型转换:
        console.log("++++++++++++++++++++++++++++++++++")
        console.log(Boolean([]));//true
        console.log(Boolean({}));//true
        console.log(Number([]));//0
        console.log(Number({}));//NAN
       console.log('undefined', undefined ? '1' : '2') // 2
       console.log('null', null ? '1' : '2') // 2
       console.log('object', {} ? '1' : '2') // 1
       console.log('array', [] ? '1' : '2') // 1
    
    在进行判断的时候,首先对等号左右进行数值转换!!!
    
        console.log(undefined === undefined);//true
        console.log(null === null);//true
        console.log(undefined === null);//false
        console.log(undefined == null);//true
    总结:undefined、null与自己比较返回true;两者比较,相等但不全等。
    
        console.log("=====================");
        console.log(undefined == false);//false
        console.log(undefined == true);//false
        console.log(null == false);//false
        console.log(null == true);//false
    总结:undefined、null既不等于true,也不等于false。
    
        console.log("======================");
        console.log(false == 0);//true
        console.log("======================");
        console.log(null == 0);//false
        console.log([] == 0);//true
        console.log({} == 0);//false
    总结:空数组与0相等;但是空对象与0不相等。
    
        console.log("======================");
        console.log(null == false);//false
        console.log([] == false);//true
        console.log({} == false);//false
    总结:空数组与0相等,false与0相等,所以空数组等于false;空对象与0不相等,false与0相等,所以空对象与false不相等。
    
        console.log("======================");
        console.log(([])?true:false);//true
        console.log(({})?true:false);//true
        console.log(([]==false)?true:false);//true
        console.log(({}==false)?true:false);//false
  • 相关阅读:
    js模版引擎handlebars.js实用教程——由于if功力不足引出的Helper
    js模版引擎handlebars.js实用教程——if-判断的基本用法
    js模版引擎handlebars.js实用教程——with-终极this应用
    js模版引擎handlebars.js实用教程——with-进入到某个属性(进入到某个上下文环境)
    js模版引擎handlebars.js实用教程——each-循环中使用this
    mysql 索引及查询优化总结
    面试篇——mysql
    设计模式六大原则(5):迪米特法则
    BigInteger与BigDecimal
    Java基本类型占用字节数(或 bit数)
  • 原文地址:https://www.cnblogs.com/minyDong/p/11530715.html
Copyright © 2020-2023  润新知