转圈圈

javascript 数据类型

思维导图 : javascript数据类型

基本数据类型

  var a =1 // "number"

  var b ="" // "string"

  var c =true // "boolean"

  
  var e // "undefined"

  var s = Symbol() // "symbol"

var d = null // "object"
  
// 注意
  null == undefined // true
  null == {} // false
  undefined == {} //false

引用数据类型

  var o = {"a":1} // "object"

数据类型判断

  1. typeof

     typeof null  //   "object"
     
     typeof [] //    "object"
    
     typeof {} //   "object"
    
  2. instanceof

    不能检测 null undefined

     // object
    ({}) instanceof Object //    true
    
     //null
     null instanceof Object //    false
    
     //array
     [] instanceof Array //     true
    
     //error
     null instanceof Null
     VM1344:1 Uncaught ReferenceError: Null is not defined
         at <anonymous>:1:17
     (anonymous) @ VM1344:1
     undefined instanceof Undefined
     VM1366:1 Uncaught ReferenceError: Undefined is not defined
         at <anonymous>:1:22
    
  3. Object.protorype.toString.call() 最靠谱,最常用

     Object.prototype.toString.call("fd") //     "[object String]"
     Object.prototype.toString.call(1) //     "[object Number]"
     Object.prototype.toString.call(true) //     "[object Boolean]"
     Object.prototype.toString.call(null) //     "[object Null]"
     Object.prototype.toString.call(undefined) //     "[object Undefined]"
     Object.prototype.toString.call(Symbol()) //     "[object Symbol]"
     Object.prototype.toString.call({}) //     "[object Object]"
    
posted @ 2019-04-19 23:33  rosendolu  阅读(131)  评论(0编辑  收藏  举报