• js数据类型


    数据类型:数值型number,布尔型boolean,字符串型string,对象object,数组array、,空值null,未定义undefined

    验证数据类型:

    一、typeof方式

    在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 "object"。

    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'

    console.log(typeof {}) //'object' 这个应该是json

    console.log(typeof [])//'object'   这个应该是数组 却被认为了是对象

    console.log(typeof unknownVariable) //'undefined'

    json对象和数组返回的都是对象,所以通过typeof验证不了

    二、toString方式(toString其实是object里面的方法)

    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]

    toString.call(); < ==>Object.prototype.toString.call();

    console.log(Object.prototype.toString.call(arr) === '[object Array]');//返回的是true

    三、instanceof 

    判断已知对象类型的方法: instanceof

    console.log('数据类型判断 - instanceof')

    var arr=[] var arr = new Array()

    console.log(arr instanceof Array) //---------------> true

    console.log(date instanceof Date) //---------------> true

    console.log(fn instanceof Function) //------------> true

    // alert(f instanceof function) //------------> false

    // 注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

  • 相关阅读:
    特征可视化技术(CAM)
    特征可视化技术(CAM)
    attention
    attention
    Aggregated Residual Transformations for Deep Neural Networks(ResNeXt)
    attention
    attention
    位姿估计
    attention
    通过几个例子看下seajs模块标识符(有图有真相)
  • 原文地址:https://www.cnblogs.com/ccvtt/p/8317077.html
Copyright © 2020-2023  润新知