zoukankan      html  css  js  c++  java
  • 数据类型的判断

    /******************************************************************************
    数据类型判断 - typeof
    *******************************************************************************/
    console.log('数据类型判断 - typeof')
    console.log(typeof undefined)//'undefined'
    console.log(typeof null) // well-known bug
    console.log(typeof true) //'boolean'
    console.log(typeof 123) //'number'
    console.log(typeof "abc") //'string'
    console.log(typeof function() {}) //'function'
    var arr=[];
    console.log(typeof {}) //'object'
    console.log(typeof arr)//'object'
    console.log(typeof unknownVariable) //'undefined'
    // 在使用 typeof 运算符时采用引用类型存储值会出现一个问题,
    // 无论引用的是什么类型的对象,它都返回 "object"。
    

      

    /******************************************************************************
    数据类型判断 - toString.call
    通用但很繁琐的方法: prototype
    *******************************************************************************/
    console.log('数据类型判断 - toString.call')
    console.log(toString.call(123)) //[object Number]
    console.log(toString.call('123')) //[object String]
    console.log(toString.call(undefined)) //[object Undefined]
    console.log(toString.call(true)) //[object Boolean]
    console.log(toString.call({})) //[object Object]
    console.log(toString.call([])) //[object Array]
    console.log(toString.call(function(){})) //[object Function]
    
    console.log(Object.prototype.toString.call(str) === '[object String]') //-------> true;
    console.log(Object.prototype.toString.call(num) === '[object Number]') //-------> true;
    console.log(Object.prototype.toString.call(arr) === '[object Array]') //-------> true;
    console.log(Object.prototype.toString.call(date) === '[object Date]') //-------> true;
    console.log(Object.prototype.toString.call(fn) === '[object Function]') //-------> true;
    

      







    /******************************************************************************
    数据类型判断 - instanceof
    *******************************************************************************/
    // 判断已知对象类型的方法: instanceof
    console.log('数据类型判断 - instanceof')
    
    console.log(arr instanceof Array) //---------------> true
    console.log(date instanceof Date) //---------------> true
    console.log(fn instanceof Function) //------------> true
    // alert(f instanceof function) //------------> false
    // 注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
    

      

    /******************************************************************************
    数据类型判断 - 根据对象的constructor判断: constructor
    *******************************************************************************/
    // 根据对象的constructor判断: constructor
    var arr=[];
    console.log('数据类型判断 - constructor')
    console.log(arr.constructor === Array) //----------> true
    console.log(date.constructor === Date) //-----------> true
    console.log(fn.constructor === Function) //-------> true
    

      

    // jQuery提供一系列工具方法,用来判断数据类型,以弥补JavaScript原生的typeof运算符的不足。
    // 以下方法对参数进行判断,返回一个布尔值。
    
    // jQuery.isArray():是否为数组。
    // jQuery.isEmptyObject():是否为空对象(不含可枚举的属性)。
    // jQuery.isFunction():是否为函数。
    // jQuery.isNumeric():是否为数字。
    // jQuery.isPlainObject():是否为使用“{}”或“new Object”生成的对象,而不是浏览器原生提供的对象。
    // jQuery.isWindow():是否为window对象。
    // jQuery.isXMLDoc():判断一个DOM节点是否处于XML文档之中。
    

      

  • 相关阅读:
    spark基于zookeeper的高可用异常启动分析
    cdh 系统配置文件更新 error parsing conf core-site.xml
    spark2.1消费kafka0.8的数据 Recevier && Direct
    spark2.3 消费kafka0.10数据
    hadoop3.x的HA解析
    nginx安装运维
    redhat7 升级openssh openssl
    kylin 密码错误
    Vray5 材质库 图灵炸裂版 (支持Vray3.x) + VMMP
    让3dmax第一次打开材质浏览器不要卡顿
  • 原文地址:https://www.cnblogs.com/lhy-93/p/5741655.html
Copyright © 2011-2022 走看看