1. 程式人生 > >js變數型別判斷 嚴格通用 Object.prototype.toString.call()

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]"

typeof判斷結果:

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