JS判断数据类型

一、typeof

typeof可以检测基本数据类型,但是碰到引用数据类型返回的都是object。

typeof通常用来判断基本数据类型,但是用typeof来判断null和引用类型的实例返回的结果都是"object"

let s = "Nicholas";
let b = true;
let i = 22;
let u;
let n = null;
let o = new Object();
function Fn() {}
console.log(typeof s); // string
console.log(typeof i); // number
console.log(typeof b); // boolean
console.log(typeof u); // undefined
console.log(typeof n); // object
console.log(typeof o); // object
console.log(typeof new Fn()); // object
console.log(typeof Fn); // function

二、instanceof

instanceof可以用于引用类型的检测,但是不能检测出非构造函数定义的基本数据类型,不能检测出null和undefined。

使用instanceof,如:a instanceof A,判断参照对象(A)的prototype属性所指向的对象是否在对象的a的原型链上,instanceof只能用来判断两个对象是否属于实例关系,而不能判断一个对象实例具体属于哪种类型,例如:

function X(name,age){
  this.name = name;
  this.age = age;
}
 
a = new X('xx',18);
console.log(a instanceof X)  //true
 
 
obj = new Object()//创建一个空对象obj
//或者通过字面量来创建:
obj = {}
console.log(obj instanceof Object); // true
 
arr = new Array()  //创建一个空数组arr  或arr = []
console.log(arr instanceof Array ); // true
 
date = new Date()
console.log(date instanceof Date ); // true

简单实现instanceof

function my_instanceof(L, R) {
  const O = R.prototype;
  if (L === null) {
    return false;
  }
  L = L.__proto__;
  while (true) {
    if (L === null) {
      return false;
    }
    if (L === O) {
      return true;
    }
    L = L.__proto__;
  }
}

instanceof的弊端:对于number、string、boolean这三种基本数据类型,只有通过构造函数定义才能检测出来,如:let num1 = new Number(1);这样定义才能检测出来,let num1 = 1;这样定义检测不出来。

三、根据constructor判断

constructor可以检测js的基本类型和引用类型,不能检测出undefined和null。

针对于instanceof的弊端,我们使用constructor检测,constructor是原型对象的属性指向构造函数。

 
let num = 23;
let date = new Date();
let str = "biu~";
let reg = new RegExp();
let bool = true;
let fn = function () {
  console.log(886);
};
let udf = undefined;
let nul = null;
let array = [1, 2, 3];
console.log(num.constructor); // [Function: Number]
console.log(date.constructor); // [Function: Date]
console.log(str.constructor); // [Function: String]
console.log(bool.constructor); // [Function: Boolean]
console.log(fn.constructor); // [Function: Function]
console.log(reg.constructor); // [Function: RegExp]
console.log(array.constructor); // [Function: Array]
console.log(udf.constructor);//Cannot read property "constructor" of undefined
console.log(nul.constructor);//Cannot read property "constructor" of null

四、Object.prototype.toString.call:可以检测js所有数据类型

《你不知道的javaScript》(中卷):所有typeof返回值为"object"的对象,都包含一个内部属性[[Class]],我们可以把他看作一个内部的分类,而非传统意义上面向对象的类,这个属性无法直接访问,一般通过Object.prototype.toString(…)来查看。并且对于基本数据类类型null,undefined这样没有原生构造函数,内部的[[Class]]属性值仍然是Null和Undefined 

Object.prototype.toString.call();
console.log(toString.call(123));          //[object Number]
console.log(toString.call('123'));        //[object String]
console.log(toString.call(undefined));    //[object Undefined]
console.log(toString.call(true));         //[object Boolean]
console.log(toString.call({}));           //[object Object]
console.log(toString.call([]));           //[object Array]
console.log(toString.call(function(){})); //[object Function]

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值