constructor判斷數據類型

我們可以通過constructor來判斷數據的類型,但是除了null、undefined,因爲他們不是由對象構建。

數字、布爾值、字符串是包裝類對象,所以有constructor

數字
var num = 1;
num.constructor
ƒ Number() { [native code] }

布爾值
true.constructor
ƒ Boolean() { [native code] }

字符串
"".constructor
ƒ String() { [native code] }

函數
var func = function(){}
func.constructor
ƒ Function() { [native code] }

數組
[].constructor
ƒ Array() { [native code] }

對象
var obj = {}
obj.constructor
ƒ Object() { [native code] }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章