• js变量类型判断 严格通用 Object.prototype.toString.call()


    Object.prototype.toString.call()判断结果:

    Object.prototype.toString.call(true)
    "[object Boolean]"
    Object.prototype.toString.call(1)
    "[object Number]"
    Object.prototype.toString.call(null)
    "[object Null]"
    Object.prototype.toString.call(undefined)
    "[object Undefined]"
    Object.prototype.toString.call('ndefined')
    "[object String]"
    Object.prototype.toString.call([])
    "[object Array]"
    Object.prototype.toString.call({})
    "[object Object]"
    Object.prototype.toString.call(alert)
    "[object Function]"
    Object.prototype.toString.call(/[a-z]/)
    "[object RegExp]"
    

    typeof判断结果:

    typeof undefined
    "undefined"
    typeof true
    "boolean"
    typeof 1
    "number"
    typeof 'sds'
    "string"
    typeof alert
    "function"
    typeof null
    "object"
    typeof []
    "object"
    typeof {}
    "object"
    typeof /[a-z]/
    "object"
    

    特别的:(typeof)

    new Array(); new Object(); new Boolean(); new RegExp("[a-z]","g");是类似的~

    var a = new String('123');
    console.log(a);
    console.log(Object.prototype.toString.call(a));
    console.log(typeof a);
    ---
    var b = new Number(123);
    console.log(b);
    console.log(Object.prototype.toString.call(b));
    console.log(typeof b);
    

    结果为:

    String {"123"}
    [object String]
    object
    ---
    Number {123}
    [object Number]
    object
    
  • 相关阅读:
    Winform跨线程操作界面的策略
    Winform DataGridView扩展
    GDI+的常用类
    函数中参数的验证顺序
    C# Winform常见的Editor及其它经验
    能够引起异常的运算符和关键字
    终结程序
    C#异常处理策略
    python3.6入门到高阶(全栈) day01 python 基础
    虚拟机中系统盘扩容
  • 原文地址:https://www.cnblogs.com/ysk123/p/9996034.html
Copyright © 2020-2023  润新知