30、JavaScript的基本类型和引用类型的判断方法

一、常见的基本数据类型和引用数据类型

1、基本数据类型:Number、String、Boolean、Null、undefined。基本数据类型,名字和值都会储存在栈内存中。

2、引用数据类型:Object、Array、Function、Date、RegExp、Error等。

二、数据类型的4种判断方法

typeof、instanceof、constructor、Object.prototype.toString.call()

1、typeOf:基本数据类型(除了Null类型,返回’object’)都能被准确检测,并返回相应的字符串,大部分的引用类型都不能被准确检测(除了Function引用数据类型,返回’function’),其他的引用类型都返回’object’字符串。

	console.log(typeof 1); // 'number'
    console.log(typeof 'lunlun'); // 'string'
    console.log(typeof false); // 'boolean'
    console.log(typeof function () {}); // 'function'
    console.log(typeof undefined); // 'undefined'
    console.log(typeof {}); // 'object'
    console.log(typeof []); // 'object'
    console.log(typeof null); // 'object'

2、instanceof(只能准确判断引用类型):instanceof原理为 ‘只要右边变量的prototype在左边变量的原型链上即可’。如 a instanceof A 判断参照对象 A 的prototype属性所指向的对象是否在被测试对象a的原型链上,instanceof 只能用来判断两个对象是否属于实例关系,而不能判断一个对象实例具体属于哪种类型。

	console.log(7 instanceof Number); // false
    console.log('7' instanceof String); // false
    console.log(false instanceof Boolean); // false
    console.log([] instanceof Array); //true
    console.log(function () {} instanceof Function); // true
    console.log({} instanceof Object); // true
    let date = new Date()
	console.log(date instanceof Date ); // true
    // undefined is not a constructor
    // null is not a constructor
弊端:对于number,string,boolean这三种基本数据类型无法准确判断其类型,只能通过构造函数定义才能检测出其类型
let num =new Number(1);
console.log(num instanceof Number) //true

3、constructor(null和undefined不能被检测,没有原生构造函数):constructor指的就是对象的构造函数。

let num = 123;
let str = "biu~";
let date = new Date();
let reg = new RegExp();
let bool = true;
let fn = function () {};
let array = [1, 2, 3];
let obj = {} 
console.log(num.constructor === Number)//true
console.log(str.constructor === String)//true
console.log(bool.constructor === Boolean)//true
console.log(date.constructor === Date)//true
console.log(reg.constructor === RegExp)//true
console.log(fn.constructor === Function)//true
console.log(array.constructor === Array)//true
console.log(obj.constructor === Object)//true

4、Object.prototype.toString.call():通过Object.prototype下的toString.call()方法来判断类型,对于基本数据类类型null,undefined这样没有原生构造函数,可以通过Object.prototype.toString.call()来进行判断。

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(null));    	  //'[object Null]'
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]'
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值