Bin's blog Bin's blog
首页
  • Java生态

    • Java
    • JVM虚拟机
    • 数据结构与算法
  • Spring生态

    • SSM
    • SpingBoot
  • 微服务

    • Spring Cloud
    • Spring Cloud alibaba
  • 分布式

    • 消息队列
    • ELK
    • 分布式事务
    • 其他
  • 数据库

    • 关系数据库
    • NoSql
  • 项目管理

    • Git
    • Maven
  • 《HTML》
  • 《CSS》
  • 《JavaScript》
  • 《ES6》
  • 《Vue》
  • 《React》
  • 《TypeScript》
  • 《Git》
  • 《小程序》
  • Linux
  • Docker容器
  • Kubernetes
  • Nginx
  • Jenkins
  • Devops
  • 项目实战

    • 业务实战
    • 云计算产品
    • 开放对接
  • 计算机原理
  • 计算机网络
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Bin Sun

Java萌新
首页
  • Java生态

    • Java
    • JVM虚拟机
    • 数据结构与算法
  • Spring生态

    • SSM
    • SpingBoot
  • 微服务

    • Spring Cloud
    • Spring Cloud alibaba
  • 分布式

    • 消息队列
    • ELK
    • 分布式事务
    • 其他
  • 数据库

    • 关系数据库
    • NoSql
  • 项目管理

    • Git
    • Maven
  • 《HTML》
  • 《CSS》
  • 《JavaScript》
  • 《ES6》
  • 《Vue》
  • 《React》
  • 《TypeScript》
  • 《Git》
  • 《小程序》
  • Linux
  • Docker容器
  • Kubernetes
  • Nginx
  • Jenkins
  • Devops
  • 项目实战

    • 业务实战
    • 云计算产品
    • 开放对接
  • 计算机原理
  • 计算机网络
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 《HTML》笔记

  • 《CSS》笔记

  • 《JavaScript》笔记

    • 基础
    • 内置对象
    • 面向对象
    • 异步操作
    • DOM
    • 事件
    • 浏览器模型
    • JS设计模式总结笔记
    • 《JavaScript高级程序设计》笔记
    • JavaScript文章

      • 33个非常实用的JavaScript一行代码
      • new命令原理
      • ES5面向对象
      • ES6面向对象
      • 多种数组去重性能对比
      • JS随机打乱数组
      • 判断是否为移动端浏览器
      • 将一维数组按指定长度转为二维数组
      • 防抖与节流函数
      • JS获取和修改url参数
      • 比typeof运算符更准确的类型判断
  • 《ES6》笔记

  • 《Node》笔记

  • 《Vue》笔记

  • 《React》笔记

  • 《TypeScript》笔记

  • 《小程序》笔记

  • 《Git》笔记

  • 前端
  • 《JavaScript》笔记
  • JavaScript文章
sunbin
2020-04-13

比typeof运算符更准确的类型判断

# 比typeof运算符更准确的类型判断

不同数据类型的Object.prototype.toString方法返回值如下。

  • 数值:返回[object Number]。
  • 字符串:返回[object String]。
  • 布尔值:返回[object Boolean]。
  • undefined:返回[object Undefined]。
  • null:返回[object Null]。
  • 数组:返回[object Array]。
  • arguments 对象:返回[object Arguments]。
  • 函数:返回[object Function]。
  • Error 对象:返回[object Error]。
  • Date 对象:返回[object Date]。
  • RegExp 对象:返回[object RegExp]。
  • 其他对象:返回[object Object]。

这就是说,Object.prototype.toString可以看出一个值到底是什么类型。

Object.prototype.toString.call(2) // "[object Number]"
Object.prototype.toString.call('') // "[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(Math) // "[object Math]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call([]) // "[object Array]"
1
2
3
4
5
6
7
8

利用这个特性,可以写出一个比typeof运算符更准确的类型判断函数。

var type = function (o){
    var s = Object.prototype.toString.call(o)
    return s.match(/\[object (.*?)\]/)[1].toLowerCase()
}
type({}); // "object"
type([]); // "array"
type(5); // "number"
type(null); // "null"
type(); // "undefined"
type(/abcd/); // "regex"
type(new Date()); // "date"
1
2
3
4
5
6
7
8
9
10
11

在上面这个type函数的基础上,还可以加上专门判断某种类型数据的方法。

var type = function (o){
  var s = Object.prototype.toString.call(o);
  return s.match(/\[object (.*?)\]/)[1].toLowerCase();
};

['Null',
 'Undefined',
 'Object',
 'Array',
 'String',
 'Number',
 'Boolean',
 'Function',
 'RegExp'
].forEach(function (t) {
  type['is' + t] = function (o) {
    return type(o) === t.toLowerCase();
  };
});

type.isObject({}) // true
type.isNumber(NaN) // true
type.isRegExp(/abc/) // true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
编辑 (opens new window)
上次更新: 2024/05/28, 08:37:26
JS获取和修改url参数
ECMAScript 6 简介

← JS获取和修改url参数 ECMAScript 6 简介→

最近更新
01
计算机网络
05-27
02
计算机原理
05-27
03
Devops
05-27
更多文章>
Theme by Vdoing | Copyright © 2024-2024 Bin Sun | MIT License | 苏公网安备32021402002768| 苏ICP备2024096492号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式