js中的数据类型检测

js数据类型检测的几种方法

一、typeof (检测基本类型值)

  • 使用时直接 typeof 要检测的类型值,比如
typeof 123 //number
  • typeof 可以检测 number、string、boolean、null、undefined、symbol、Bigint这些基本类型值。
  • 对于引用类型只可以检测出object(包括不同对象、数组、正则、日期等等)、function等 也就是说不管对象类型具体为什么类型都显示为 object比如:
typeof [1,2,3]//Object
  • 并且typeof 返回的结果都是字符串,对于null的检测结果也为object(遗留问题,与instanceof不同)

二、instanceof(检测对象类型)

  • 使用时 [要检测的数据] instanceof [要验证的对象类型]比如
let testArr = [1, 2, 3]
console.log(testArr instanceof Array)//true
  • 该方法返回一个boolean数据,来判断是否属于数据该类
  • 原理就是通过原型链来判断实例的__proto__是否与检测类型的prototype一致,比如
let testArr = [1, 2, 3];
console.log(testArr instanceof Array); //true
console.log(
  testArr.__proto__ === Array.prototype,
  testArr.__proto__.__proto__ === Object.prototype
); //true true

  • instanceof无法检测基本类型值,比如一个number、string等,除非是新建基本类的实例,如下所示
console.log(123 instanceof Number); //false
let n = new Number(123);
let s = new String("abc");
console.log(n instanceof Number, s instanceof String); //true true

三、constructor (检测对象类型)

  • 使用时就[要判断的类型值].constructor === [需要验证的类],比如
console.log([1, 2, 3].constructor === Array); //true
  • 原理也是通过原型链,我们知道原型prototype上都有一个constructor属性指向其构造函数。所以实例获取constructor时,自身没有这个属性,向原型上找然后比对,也无法判断基本类型值

四、Object的toString方法(全能)

  • 使用时Object.prototype.toString.call(需要判断的值),通过call来改变Object的toString方法的this指向,从而达到判断类型的效果。比如
//此方法即可以判断值类型,也可以判断引用类型值,比较常用
console.log(
  Object.prototype.toString.call(123),
  Object.prototype.toString.call([1, 2, 3]),
  Object.prototype.toString.call({ a: 123 })
);//[object Number] [object Array] [object Object]

关于call和apply和bind方法另出一篇笔记文章

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript,有七种基本数据类型和一种复杂数据类型。 1. 基本数据类型: - number:数字类型,包括整数和浮点数 - string:字符串类型 - boolean:布尔类型,true或false - undefined:未定义类型 - null:空类型 - symbol:符号类型,ES6新增 - bigint:大整数类型,ES10新增 2. 复杂数据类型: - object:对象类型,包括数组、函数、正则表达式等 检测数据类型的方法: 1. typeof运算符 typeof是JavaScript的一元运算符,可以返回一个值的数据类型。例如: ```javascript typeof 123; // "number" typeof "hello"; // "string" typeof true; // "boolean" typeof undefined; // "undefined" typeof null; // "object" typeof Symbol(); // "symbol" typeof 100n; // "bigint" ``` 需要注意的是,typeof null返回的是"object",这是一个历史遗留问题。 2. instanceof运算符 instanceof运算符用于检测一个对象是否属于某个类或构造函数的实例。例如: ```javascript const arr = [1, 2, 3]; arr instanceof Array; // true const fn = function() {}; fn instanceof Function; // true ``` 需要注意的是,instanceof只能用于检测对象类型,不能检测基本数据类型。 3. Object.prototype.toString方法 Object.prototype.toString方法可以返回一个值的完整数据类型。例如: ```javascript Object.prototype.toString.call(123); // "[object Number]" Object.prototype.toString.call("hello"); // "[object String]" 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(Symbol()); // "[object Symbol]" Object.prototype.toString.call(100n); // "[object BigInt]" ``` 需要注意的是,null和undefined没有自己的构造函数,所以通过Object.prototype.toString方法检测它们的类型需要特殊处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值