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

猜你喜欢

转载自blog.csdn.net/zyz00000000/article/details/106403998