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
    
  • 相关阅读:
    SQLalchemy 字段类型
    爬虫学习
    Linux了解一下
    django-rest-framework
    vue相关理论知识
    Django认证系统
    Form组件
    JS之AJAX
    Django之中间件
    Django之ORM
  • 原文地址:https://www.cnblogs.com/ysk123/p/9996034.html
Copyright © 2011-2022 走看看