JavaScript判断数据类型的方法及封装函数判断数据类型

首先先回顾一下javascript的数据类型都有哪些?


基本数据类型:number,undefined,boolean,string,null
复杂数据类型:object
另外ES6中又新增了一个数据类型:Symbol

  1. undefined

    Undefined类型只有一个值,他就是undefined,声明变量但未赋值,这个变量的值就是undefined。

  2. null

    null就是声明了变量并给其赋值为null

  3. boolean
    该类型有两个可取值true和false

  4. string
    字符串

  5. number
    数值,跟其他语言不同的是他没有去细分是整数还是小数

  6. object
    object、Function、Date、Array、RegExp、Boolean、Number、String

  7. Symbol
    表示一个独一无二的值

接下来我们来看一下一些判断数据类型的方法

一、typeof()函数判断基本数据类型

console.log(typeof 100); //number
console.log(typeof '100'); //string
console.log(typeof true); //boolean
console.log(typeof null); //object
console.log(typeof undefined); //undefined
console.log(typeof []); //object
console.log(typeof {}); //object

可以看出上面代码中判断[]和{}都是object,所以typeOf()只是适合判断基本的数据类型
注意:为什么null打印出来也是object,因为null被认为是对象是占位符

二、instanceof

console.log({} instanceof Object); // true
console.log([] instanceof Array); // true
console.log(new Date() instanceof Date); // true
console.log(function () { } instanceof Function); // true
console.log('123' instanceof String); // false

由上述代码看出instanceof对于引用类型的类型检测支持很好,但是无法对基本类型数据进行类型检测。

三、Object.prototype.toString.call()

 //1、基本数据类型
var num1 = 1;
var num2 = new Number(1);
console.log(Object.prototype.toString.call(num1) == '[Object Number]'); // true
console.log(Object.prototype.toString.call(num2) == '[Object Number]'); // true
//2、数组
console.log(Object.prototype.toString.call([]) == '[Object Array]') // true
//3、函数
console.log(Object.prototype.toString.call(function(){})=='[Object Function]')// true
//4、自定义对象
function P() { }
console.log(Object.prototype.toString.call(new P()) == '[Object Object]') // true

Object.prototype.toString.call()对于基本类型和引用类型都可以判断(除了自定义的类)

接下来让我们来封装一个函数判断数据类型

function getDataType(data) {
  if (Array.isArray(data)) {
    return 'array';
  } else if (typeof data === 'object' && data !== null) {
    return 'object';
  } else if (typeof data === 'string') {
    return 'string';
  } else if (typeof data === 'number') {
    return 'number';
  } else if (typeof data === 'boolean') {
    return 'boolean';
  } else if (typeof data === 'function') {
    return 'function';
  } else if (data instanceof Date) {
    return 'date';
  } else if (typeof data === 'undefined') {
    return 'undefined';
  } else if (data === null) {
    return 'null';
  } else {
    return 'unknown';
  }
}

// 示例用法
console.log(getDataType('Hello World')); // 输出: string
console.log(getDataType(42)); // 输出: number
console.log(getDataType(true)); // 输出: boolean
console.log(getDataType({})); // 输出: object
console.log(getDataType([])); // 输出: array
console.log(getDataType(function() {})); // 输出: function
console.log(getDataType(new Date())); // 输出: date
console.log(getDataType(undefined)); // 输出: undefined
console.log(getDataType(null)); // 输出: null
console.log(getDataType(Symbol('foo'))); // 输出: unknown

可以将要判断的数据作为参数传递给getDataType函数,然后它将返回数据的类型。该函数可以对大多数常见的数据类型进行判断,包括数组、对象、字符串、数字、布尔、函数、日期、undefined、null,并且对于其他不常见的类型,将返回’unknown’。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GG--Bond

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值