当前位置:网站首页>判断数据的精确类型
判断数据的精确类型
2022-07-27 14:39:00 【前行的枫】
Object.prototype.toString.call(value)
这不是toString方法吗,转成字符串吗?
其实toString()是object原型上的方法,所有引用类型都能继承它,它返回的是"[object type]",type为object对象类型。

既然引用类型都有这个方法,为什么返回的形式并不都是"[object type]"呢?再来看看mdn
发现大部分重写了toString()方法,也就是说比如数组的原型链有两个toString方法,那么就会继承原型链上离自己最近的方法,所以数组的toString()是转成字符串。要想调用object原型链上的toString(),可以改变this的指向:Object.prototype.toString.call(value)
instanceof :运算符用于某个实例对象的原型链上是否存构造函数的 prototype 属性
typeof : 判断数据类型
这两种方法在判断数据的精确类型上都不准确,如果只是需要判断基本数据类型,typeof就足够。
边栏推荐
- Division of entity classes (VO, do, dto)
- Talk about ThreadLocal
- 【云享读书会第13期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
- Learn parquet file format
- C language: Sanzi game
- 多线程带来的的风险——线程安全
- CAS compares the knowledge exchanged, ABA problems, and the process of lock upgrading
- C language: string function and memory function
- Spark 3.0 testing and use
- 一款功能强大的Web漏洞扫描和验证工具(Vulmap)
猜你喜欢

DRF学习笔记(五):视图集ViewSet

C language: string function and memory function

Spark troubleshooting finishing

Implement custom spark optimization rules

The difference between synchronized and reentrantlock

First understanding of structure
![[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN](/img/01/bbf81cccb47b6351d7265ee4a77c55.png)
[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN

juc包下常用工具类
![[sword finger offer] interview question 45: arrange the array into the smallest number](/img/d1/e2e21c95ecf5b8e3854f134cc47228.png)
[sword finger offer] interview question 45: arrange the array into the smallest number

CAS compares the knowledge exchanged, ABA problems, and the process of lock upgrading
随机推荐
C: On function
Clickhouse 20.x distributed table testing and chproxy deployment (II)
Division of entity classes (VO, do, dto)
面试重点——传输层的TCP协议
聊聊面试必问的索引
Addition, deletion, query and modification of MySQL table data
剑指 Offer 51. 数组中的逆序对
线程中死锁的成因及解决方案
【云享读书会第13期】音频文件的封装格式和编码格式
Flask连接mysql数据库已有表
【云享读书会第13期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
C language: custom type
DRF学习笔记(五):视图集ViewSet
Talk about the index of interview questions
[Yunxiang book club issue 13] coding format of video files
[sword finger offer] interview question 54: the k-largest node of the binary search tree
网络设备硬核技术内幕 路由器篇 小结(下)
Hj8 consolidated statement record
Understand │ what is cross domain? How to solve cross domain problems?
Extended log4j supports the automatic deletion of log files according to time division and expired files