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
    

      

  • 相关阅读:
    禅道项目管理系统自定义菜单相关
    2015年技术方向转变计划
    LinuxMint 17.1 Cinnamon桌面窗口焦点bug
    通过指定函数/方法形参类型提高PHP代码可靠性
    Apache+Mod_Python配置
    JPHP最新进展 v0.6
    “领域驱动开发”实例之旅(1)--不一样的开发模式
    Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作
    哈希值 是什么?哈希值是什么东西啊?具体怎么识别?怎么用?
    TortoiseGit 使用教程
  • 原文地址:https://www.cnblogs.com/matthew9298-Begin20160224/p/12419563.html
Copyright © 2011-2022 走看看