zoukankan      html  css  js  c++  java
  • JavaScript类型判断

    整理一下js里面的类型判断

    <script>
    let boolType = true;
    let numberType = 1;
    let stringType = 'abc';
    let undefinedType = undefined;
    let nullType = null;
    let arrayType = [1,2,3,4];
    let objectType = {name:'xiaoming',age:22};
    let functionType = function(){console.log('hello')};
    let symbolType = Symbol();
    let nanType = NaN;
    /***
    操作符typeof
    不能识别null,Array,都把它统一归为object类型
    ***/
    console.log('-----------------typeof-----begin-------------------')
    console.log('bool',typeof boolType); //boolean
    console.log('number',typeof numberType);//number
    console.log('string',typeof stringType);//string
    console.log('undefined',typeof undefinedType);//undefined
    console.log('null',typeof nullType);//object
    console.log('array',typeof arrayType);//object
    console.log('object',typeof objectType);//object
    console.log('function',typeof functionType);//function
    console.log('symbol',typeof symbolType); //symbol
    console.log('nan',typeof nanType); //number
    console.log('-----------------typeof-----end---------------------')
    /***
    instanceof
    通过原型链判断
    不能识别出基本的数据类型 number、boolean、string、undefined、unll、Symbol
    ***/
    console.log('-----------------instanceof-----begin-------------------')
    console.log('bool',boolType instanceof Boolean); 
    console.log('number',numberType instanceof Number);
    console.log('string',stringType instanceof String);
    console.log('undefined',undefinedType instanceof Object);
    console.log('null',nullType instanceof Object);
    console.log('array',arrayType instanceof Array);
    console.log('object',objectType instanceof Object);
    console.log('function',functionType instanceof Function);
    console.log('symbol',symbolType instanceof Symbol);
    console.log('nan',nanType instanceof Number);
    console.log('-----------------instanceof-----end---------------------')
    /***
    constructor
    返回对创建此对象的函数的引用
    但是可以人为的创建一个对象,更改它的原型
    null、undefined没有construstor方法,因此constructor不能判断undefined和null
    ***/
    console.log('-----------------constructor-----begin-------------------')
    console.log('bool',boolType.constructor === Boolean);// true
    console.log('number',numberType.constructor === Number);// true
    console.log('string',stringType.constructor === String);// true
    console.log('array',arrayType.constructor === Array);// true
    console.log('object',objectType.constructor === Object);// true
    console.log('function',functionType.constructor === Function);// true
    console.log('symbol',symbolType.constructor === Symbol);//true
    console.log('nan',symbolType.constructor === Number);//false
    console.log('-----------------constructor-----end---------------------')
    /*** 最推荐的方式
    Object.prototype.toString.call()
    运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性
    ***/
    console.log('-----------------Object.prototype.toString.call()-----begin-------------------')
    console.log('bool',Object.prototype.toString.call(boolType));//[object Boolean]
    console.log('number',Object.prototype.toString.call(numberType));//[object Number]
    console.log('string',Object.prototype.toString.call(stringType));//[object String]
    console.log('undefined',Object.prototype.toString.call(undefinedType));//[object Undefined]
    console.log('null',Object.prototype.toString.call(nullType));//[object Null]
    console.log('array',Object.prototype.toString.call(arrayType));//[object Array]
    console.log('object',Object.prototype.toString.call(objectType));//[object Object]
    console.log('function',Object.prototype.toString.call(functionType));//[object Function]
    console.log('symbol',Object.prototype.toString.call(symbolType)); //[object Symbol]
    console.log('nan',Object.prototype.toString.call(nanType)); //[object Number]
    console.log('-----------------Object.prototype.toString.call()-----end---------------------')
    /***
    NaN是个比较特别的存在怎么判断是NaN
    ***/
    console.log('nan',Object.prototype.toString.call(nanType) == '[object Number]' && nanType != nanType);// true
    

      

  • 相关阅读:
    查看客户端的IP地址,机器名,MAC地址,登陆名等信息
    查看sqlserver 2008中性能低下的语句
    搜索包含指定关键字的存储过程
    获得客户端详细信息以及每个进程的sql语句
    实战:sqlserver 日常检查脚本
    NIO的学习总结
    JavaWEB过滤器和监听器技术
    抽象工厂模式代码:
    详解 equals() 方法和 hashCode() 方法
    net.sf.json JSONObject与JSONArray使用实例
  • 原文地址:https://www.cnblogs.com/matthew9298-Begin20160224/p/12419563.html
Copyright © 2011-2022 走看看