js类型判断

1. typeof   (可准确判断原始类型,对引用类型判断不够精准)

typeof 'str' === 'string';   //string

typeof 1 === 'number';  //number

typeof undefined === 'undefined';  //undefined

typeof true === 'boolean'; //boolean

typeof function(){} === 'function'; //function

typeof {a: 0} === 'object';  //object

typeof [1, 2] === 'object';  //判断正确,但不能精准判断Array类型

typeof null === 'object'  //判断正确,但不能精准判断Null类型

typeof new Date() === 'object';  //判断正确,但不能精准判断Date类型  (其他对象类似)
var n=1,
s='hello',
b=true,
un=undefined,
nu=null,
ob={},
ar=[],
fun=function () {};
console.log(n, '类型是:'+typeof n); // 1   typeof   number
console.log(b, '类型是:'+typeof b); // true   typeof   boolean
console.log(un, '类型是:'+typeof un); // undefined   typeof   undefined
console.log(nu, '类型是:'+typeof nu); // null   typeof   object
console.log(ob, '类型是:'+typeof ob); // {}    typeof   object
console.log(ar, '类型是:'+typeof ar); // []    typeof   object
console.log(fun, '类型是:'+typeof fun); //  ƒ () {}   typeof   function

2. instanceof

console.log('str' instanceof String) // false              //#1

console.log(new String('str') instanceof String) //true     //#2

console.log(2 instanceof Number) // false                 //#3

console.log(new Number(2) instanceof Number)  //true         //#4

console.log(true instanceof Boolean)  //false           //#5

console.log(new Boolean(true) instanceof Boolean)   //true     //#6

console.log([] instanceof Array)    //true              //#7

console.log({} instanceof Object)    //true              //#8

console.log(function(){} instanceof Function)    //true        //#9

以上结果显示,直接的字面量值判断数据类型,只有引用数据类型(Array,Function,Object)被精准判断,其他(数值Number,布尔值Boolean,字符串String)字面值不能被instanceof精准判断。而instanceof 在MDN中的解释为:instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。其意思就是判断对象是否是某一数据类型(如Array)的实例。由 行#1 ~ 行#6 也可看出这个解释:字面量值'str',2, true 不是实例,所以判断值为false,而行#2、#4、#6显示 字面量值实例化后判断为true。

因此也就导致了 instanceof  用来判断 null 和 undefined 会出现问题:

如上图所示,浏览器认为null,undefined不是构造器。但是在 typeof 中你可能已经发现了,typeof null的结果是object,typeof undefined的结果是undefined ,这是怎么回事呢?尤其是null,其实这是js发展过程中设计者的重大失误,早期准备更改null的类型为null,由于当时已经有大量网站使用了null,如果更改,将导致很多网站的逻辑出现漏洞问题,就没有更改过来,于是一直遗留到现在。作为学习者,我们只需要记住就好。

3. constructor (返回对创建此对象的函数的引用)

(2).constructor === Number   //true

(true).constructor === Boolean   //true

('str').constructor === String    //true

([]).constructor === Array     //true

({}).constructor === Object    //true

(function(){}).constructor === Function   //true

从运行结果可以看出 constructor 对于类型的判断是比较精准的,但它仍然存在一个问题,当更改创建对象的原型时,会发现其类型判断也发生了改变:  

4. Object.prototype.toString.call()  (推荐使用,可准确判断对象类型)

Object.prototype.toString.call('str') === "[object String]";  //String

Object.prototype.toString.call(1) === "[object Number]";  //Number

Object.prototype.toString.call(true) === "[object Boolean]"; //Boolean

Object.prototype.toString.call(undefined) === "[object Undefined]"; //Undefined

Object.prototype.toString.call(null) === "[object Null]"; //Null

Object.prototype.toString.call({a: 0}) === "[object Object]"; //Object

Object.prototype.toString.call([0, 1, 2]) === "[object Array]"; //Array

Object.prototype.toString.call(new Date()) === "[object Date]";  //Date

Object.prototype.toString.call(() => {}) === "[object Function]" //Function

 

资料1.  https://blog.csdn.net/zjy_android_blog/article/details/81023177

资料2.  https://www.cnblogs.com/aoyo/p/5245162.html

资料3.  https://blog.csdn.net/zhangjing0320/article/details/81230170

资料4.  https://blog.csdn.net/u011272795/article/details/80105513

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值