数据类型的判断
1. typeof
查看数据类型的类型
使用
typeof obj;
返回值:"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
symbol在ES2015新引入;bigint 在ES2020新引入
注意:typeof返回值有undefined但是没有null,null返回为object
2. instanceof
查看obj是不是Obj的对象
使用
obj instanceof Obj;
返回值:true|false
注意:string 不是String,String是String
instanceof不能区别undefined和null,而且对于基本类型如果不是用new声明的则也测试不出来
3. contructor
使用
obj.instanceof===Object;
注意:constructor不能判断undefined和null,并且使用它是不安全的,因为contructor的指向是可以改变的
4. Object.prototype.toString.call()
Object.prototype.toString.call(obj);//return

浙公网安备 33010602011771号