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


    Object.prototype.toString.call(asddfff) //报错asddfff没被定义
    Object.prototype.toString.call(undefined) //"[object Undefined]"
    Object.prototype.toString.call(function a(){}) //"[object Function]"
    Object.prototype.toString.call(123) //"[object Number]"
    Object.prototype.toString.call('') //"[object String]"
    Object.prototype.toString.call() //"[object Undefined]"
    Object.prototype.toString.call(null) //"[object Null]"
    Object.prototype.toString.call([]) //"[object Array]"
    Object.prototype.toString.call({}) //"[object Object]"
    问题是:未定义的会报错(可以忽略,因为一般未定义的也不会随便拿来判断)

    typeof null // "object"
    typeof assssssaz //"undefined"
    typeof undefined //"undefined"
    typeof '' //"string"
    typeof 123 //"number"
    typeof function(){} //"function"
    typeof [] //"object"
    typeof {} //"object"
    问题是:array和null会被判断成Object。好处是:无论哪种都不会报错。

    (function a(){}) instanceof Function //true
    [] instanceof Array //true
    ({a:11}) instanceof Object //true
    123 instanceof Number //false
    '' instanceof String //false
    aaaaxsas instanceof Null //直接报错
    aaaaxsas instanceof undefined //直接报错
    问题是:这个用起来问题比较多,只能用于复杂类型的判断,不能用于基本类型的判断,未定义的变量直接报错。

  • 相关阅读:
    Leetcode 349. Intersection of Two Arrays
    hdu 1016 Prime Ring Problem
    map 树木品种
    油田合并
    函数学习
    Leetcode 103. Binary Tree Zigzag Level Order Traversal
    Leetcode 102. Binary Tree Level Order Traversal
    Leetcode 101. Symmetric Tree
    poj 2524 Ubiquitous Religions(宗教信仰)
    pat 1009. 说反话 (20)
  • 原文地址:https://www.cnblogs.com/wulinzi/p/10396257.html
Copyright © 2011-2022 走看看