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

 

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