数据类型的判断

时间:2021-09-02 00:31:36   收藏:0   阅读:11

https://segmentfault.com/a/1190000015264821

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 

原文:https://www.cnblogs.com/wlxb/p/15208364.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!