数据类型的判断

判断数据类型的方法

typeof

一般对基本类型的判断,通常使用typeof,它返回表示数据类型的字符串返回结果只能包括:number、boolean、string、function、object、undefined。

  • 缺点: 只能对基本类型进行判断,对引用值无法判断,除了函数能返回function外,其余的都返回object
console.log(
typeof [1,2,3], //"object"
typeof {a:1,b:2,c:3}, //"object"
typeof function(){console.log('aaa');}, //"function"
typeof new Date(), //"object"
typeof /^[a-zA-Z]{5,20}$/, //"object"
typeof new Error() //"object"
typeof new Number(100), //'object'
typeof new String('abc'),// 'object'
typeof new Boolean(true),//'object'
//无法分辨对象类型,只能对函数进行判断

typeof null, //"object"
//因为null与object表类型的标签都是0,因此typeof null就错误的返回了"object"

typeof 100, //"number"
typeof 'abc', //"string"
typeof false, //"boolean"
typeof undefined, //"undefined"
//对基本类型能进行判断
)

instanceof

通常在已知对象类型的情况下使用insatnceof,用来判断 A 是否为 B 的实例时,表达式为:A instanceof B,如果A是B的实例,则返回true,否则返回false。

说白了,只要右边变量的 prototype 在左边变量的原型链上即可。因此,instanceof 在查找的过程中会遍历左边变量的原型链,直到找到右边变量的 prototype,如果查找失败,则会返回 false

  • 缺点:
  1. 无法判断一个值到底属于数组还是普通对象
  2. 只能判断引用数据类型,无法判断基本数据类型
let arr = []
let obj = {}
arr instanceof Array    // true
arr instanceof Object   // true
obj instanceof Object   // true

在这个例子中,arr 数组相当于 new Array() 出的一个实例,
所以 arr.__proto__ === Array.prototype,
又因为 Array 属于 Object 子类型,
即 Array.prototype.__proto__ === Object.prototype,
所以 Object 构造函数在 arr 的原型链上

如图所示:
在这里插入图片描述
从原型链可以看出,[] 的 proto 直接指向Array.prototype, 间接指向Object.prototype, 所以按照 instanceof 的判断规则,[] 就是Object的实例

同理:


new Date() instanceof Date;//true
new Date() instanceof Object;//true

function Person(){};
new Person() instanceof Person;//true
new Person instanceof Object;//true

instanceof应用:

console.log(
100 instanceof Number, //false
'dsfsf' instanceof String, //false
false instanceof Boolean, //false        
//instanceof判断的是一个对象是否是另一个对象的实例
//以上都不是对象,只是基本数据类型,因此都是false

undefined instanceof Object, //false
null instanceof Object, //false
//因为null和undefined的类型就是自己本身,并不是Object创建出来它们,所以返回了false。

[1,2,3] instanceof Array, //true
{a:1,b:2,c:3} instanceof Object, //true
function(){console.log('aaa');} instanceof Function, //true
new Date() instanceof Date, //true
/^[a-zA-Z]{5,20}$/ instanceof RegExp, //true
new Error() instanceof Error //true
)
//注意: instanceof 后面一定要是对象类型,大小写不能写错

constructor

constructor是prototype对象上的属性,指向构造函数,它可以应对基本数据类型引用数据类型

var num = 123;
var str = 'abcdef';
var bool = true;
var arr = [1, 2, 3, 4];
var json = {name:'wenzi', age:25};
var func = function(){ console.log('this is function'); }
var und = undefined;
var nul = null;
var date = new Date();
var reg = /^[a-zA-Z]{5,20}$/;
var error= new Error();

function Person(){

}
var tom = new Person();

// undefined和null没有constructor属性

console.log(
tom.constructor==Person,
num.constructor==Number,
str.constructor==String,
bool.constructor==Boolean,
arr.constructor==Array,
json.constructor==Object,
func.constructor==Function,
date.constructor==Date,
reg.constructor==RegExp,
error.constructor==Error
);
//所有结果均为true
//除了undefined和null之外,其他类型都可以通过constructor属性来判断类型

P.S.通过constructor不仅可以判断数据类型,还可以知道这个对象叫什么名字:

var a = new Array();
console.log(a.constructor); // function Array(){..}

Object.prototype.toString.call()

toString是Object原型对象上的一个方法,它的返回值是固定的格式:一个字符串个格式的[object xxxx],xxxx代表着调用这个方法的“数据”的数据类型。
在这里插入图片描述
按理说,使用toString()方法时,返回的是这个变量的字符串显示样子:

在这里插入图片描述
我们一般采用call方法去改变Object.prototype.toString方法this的指向,因此写为:Object.prototype.toString.call(),以此来判断数据类型:

在这里插入图片描述
Object.prototype.toString.call()几乎所有的类型都能判断:

var toString = Object.prototype.toString;
toString.call(123); //"[object Number]"
toString.call('abcdef'); //"[object String]"
toString.call(true); //"[object Boolean]"
toString.call([1, 2, 3, 4]); //"[object Array]"
toString.call({name:'wenzi', age:25}); //"[object Object]"
toString.call(undefined); //"[object Undefined]"
toString.call(null); //"[object Null]"
//传入原始类型却能够判定出结果是因为对值进行了包装。
//所谓“包装对象”,指的是与数值、字符串、布尔值分别相对应的Number、String、Boolean三个原生对象。这三个原生对象可以把原始类型的值变成(包装成)对象。

toString.call(function(){ console.log('this is function'); }); //"[object Function]"
toString.call(new Date()); //"[object Date]"
toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
toString.call(new Error()); //"[object Error]"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值