zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    循序渐进学Python 1 安装与入门
    常用yum命令小结
    为CentOS配置网易163的yum源
    PHP合并数组+与array_merge的区别
    让Docker功能更强大的10个开源工具
    Docker入门系列8
    Docker入门系列7 动态映射端口port mapping
    e 的由来
    ROS教程5 使用串口
    1 ROS+ 使用ORB_SLAM2进行全场定位
  • 原文地址:https://www.cnblogs.com/ysk123/p/9996034.html
Copyright © 2011-2022 走看看