js數(shù)據(jù)類型以及其判斷方法實(shí)例
js的數(shù)據(jù)類型
基本數(shù)據(jù)類型:number , string , boolean , undefined , null , Symbol,
引用數(shù)據(jù)類型:object
NaN 屬于 number;
Function, Array, Date 都屬于 object;
基本數(shù)據(jù)類型除 null 都可以通過 typeof 判斷,引用數(shù)據(jù)類型除 Function 外都返回 Ojbect
let a = 1,
b = '2',
c = true,
d = undefined,
e = null,
f = Symbol('f'),
g = function () {},
h = [],
i = new Date()
console.log(typeof a)
console.log(typeof b)
console.log(typeof c)
console.log(typeof d)
console.log(typeof e)
console.log(typeof f)
console.log(typeof g)
console.log(typeof h)
console.log(typeof i)
查看輸出結(jié)果

可以看到 null 的 typeof 是 object , 這屬于歷史bug ,有興趣可以參考《The history of “typeof null” 》
可通過以下方法判斷 null
function checkNull(num) {
return num === null
}
object 的詳細(xì)類型可通過 Object.prototype.toString.call() 判斷
function checkObject(obj) {
return Object.prototype.toString.call(obj)
}
console.log(checkObject(g))
console.log(checkObject(h))
console.log(checkObject(i))
可看到輸出結(jié)果

也可通過構(gòu)造函數(shù) constructor() 判斷
console.log(g.constructor === Function) console.log(h.constructor === Array) console.log(i.constructor === Date)
可看到輸出結(jié)果

總結(jié)
到此這篇關(guān)于js數(shù)據(jù)類型以及其判斷方法的文章就介紹到這了,更多相關(guān)js數(shù)據(jù)類型及判斷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談javascript事件環(huán)微任務(wù)和宏任務(wù)隊列原理
這篇文章主要介紹了javascript事件環(huán) 微任務(wù)和宏任務(wù)隊列原理,幫助大家更好的理解和學(xué)習(xí)JavaScript,感興趣的朋友可以了解下2020-09-09
zepto中使用swipe.js制作輪播圖附swipeUp,swipeDown不起效果問題
Swipe JS 是一個輕量級的移動滑動組件,支持 1:1 的觸摸移動,阻力以及防滑性能都不錯,可以讓移動web應(yīng)用展現(xiàn)更多的內(nèi)容,能解決我們對于移動Web對滑動的需求。下面小編給大家介紹zepto中使用swipe.js制作輪播圖附swipeUp,swipeDown不起效果問題,需要朋友可以參考下2015-08-08
js實(shí)現(xiàn)表單提交后不重新刷新當(dāng)前頁面
本文介紹了如何通過js實(shí)現(xiàn)表單提交后不重新刷新當(dāng)前頁面的方法實(shí)例.既提交了FORM保存了數(shù)據(jù),頁面也不會跳轉(zhuǎn),很實(shí)用。需要的朋友可以參考下2016-11-11

