判断数据类型

文章介绍了在JavaScript中判断数据类型的四种常见方法:typeof操作符可以判断基础类型和function,但无法区分对象和数组;instanceof用于检查对象的原型链;通过constructor属性可以得知对象的构造函数;使用Object.prototype.toString.call能精确判断所有类型,包括原始类型和复杂类型。
摘要由CSDN通过智能技术生成

一、typeof()

适用于判断除了null外的基础类型和function

可以判断数据类型,它返回表示数据类型的字符串(返回结果只能包括number,boolean,string,function,object,undefined)
可以使用typeof判断变量是否存在(如if(typeof a!=“undefined”){…})
无法判断对象和数组,两者都返回object

typeof '5' // string
typeof 5 // number
typeof null // object 因为null被认为是空对象
typeof undefined // undefined
typeof true // boolean
typeof Symbol('5') // symbol
typeof 5n // bigint
typeof new Object(); // object
typeof new Function(); // function
或者
console.log(typeof([1,2])); //object
console.log(typeof(6)); //number
console.log(typeof(true)); //boolean

二、instanceof()

只能用来判断变量的原型链上是否有构造函数的prototype属性(两个对象是否属于原型链的关系),不一定能获取对象的具体类型

Instanceof 不适用判断原始类型的值,只能用于判断对象是否从属关系

原理:因为A instanceof B 可以判断A是不是B的实例,返回一个布尔值,由构造类型判断出数据类型

分析:[].proto 的原型 是指向Array.prototype 的,说明两个对象是属于同一条原型链的,返回true

console.log(arr instanceof Array ); // true
console.log(date instanceof Date ); // true
console.log(fn instanceof Function ); // true
//注意: instanceof 后面一定要是对象类型,大小写不能写错,该方法试用一些条件选择或分支

// 空对象的判断问题

let obj1 = {}
console.log(obj1 instanceof Object) // true
 
let obj2 = Object.create(null)
console.log(obj2 instanceof Object) // false

三、根据对象的contructor判断

原理:每一个实例对象都可通过constructor来访问它的构造函数,其实也是根据原型链的原理来的。
构造函数属性判断,返回一个布尔值。

由于undefined和null是无效的对象,因此是没有constructor属性的,这两个值不能用这种方法判断

console.log(arr.constructor === Array); //true
//或
let a = [1,2]
console.log(a.constructor === Function); //false

'5'.__proto__.constructor === String // true
[5].__proto__.constructor === Array // true
 
undefined.__proto__.constructor // Cannot read property '__proto__' of undefined
 
null.__proto__.constructor // Cannot read property '__proto__' of undefined

四、通过Object下的toString.call()方法来判断

Object.prototype.toString方法返回对象的类型字符串,因此可用来判断一个值的类型。

因为实例对象有可能会自定义toString方法,会覆盖Object.prototype.toString,所以在使用时,最好加上call。

所有的数据类型都可以使用此方法进行检测,且非常精准。

返回一个布尔值。

console.log(toString.call('123')); //[object String]
console.log(toString.call(undefined)); //[object Undefined]
console.log(toString.call({})); //[object Object]
console.log(toString.call([])); //[object Array]

Object.prototype.toString.call('5') // [object String]
Object.prototype.toString.call(5) // [object Number]
Object.prototype.toString.call([5]) // [object Array]
Object.prototype.toString.call(true) // [object Boolean]
Object.prototype.toString.call(undefined) // [object Undefined]
Object.prototype.toString.call(null) // [object Null]
Object.prototype.toString.call(new Function()); // [object Function]
Object.prototype.toString.call(new Date()); // [object Date]
Object.prototype.toString.call(new RegExp()); // [object RegExp]
Object.prototype.toString.call(new Error()); // [object Error]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值