JS数据类型检测

秃头王路飞
• 阅读 1155

数据类型检测

相信大家有的时候判断数据类型的时候会有疑惑 1、typeof、instanceof用的话会有哪些缺陷呢? 2、哪个api只要记住的话,就可以完美判断呢?

1、typeof

返回的都是字符串

type of Symbol() === 'symbol'
type of null === 'object'
type of [] === 'object'
type of {} === 'object'
type of console === 'object'
type of console.log === 'function'

undefined 为 'undefined' 可直接 typeof 判断
null 为 'object' 但并非引用类型 (null === null)
array 得另外加其他的判断条件
object 因为 null arrary 均是 object 所以也无法直接通过 typeof 去判断
function 可以直接通过 typeof 去判断

2、instanceof (instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上。)

'111' instanceof String  false
[] instanceof Array/Object   true
111 instanceof Number   false
new Date() instanceof Date/Object  true
...

3、Object.prototype.toString.call()

Object.prototype.toString.call([]) ===> [object Array]
Object.prototype.toString.call('tutouwanglufei') // "[object String]"
Object.prototype.toString.call(1) // "[object Number]"
Object.prototype.toString.call(Symbol(1)) // "[object Symbol]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(function(){}) // "[object Function]"
Object.prototype.toString.call({name: 'tutouwanglufei'}) // "[object Object]"
...

总结:

1、instanceof可以准确的判断复杂引用数据类型,但是不能正确判断基础数据类型

2、而typeof也存在弊端,他虽然可以是判断基础数据类型(null除外),但是引用数据类型中,除了function类型除外,其他也无法判断

3、用Object.prototype.toString.call()就可以完美去判断某个变量属于什么类型

点赞
收藏
评论区
推荐文章
Karen110 Karen110
2年前
一篇文章带你了解JavaScript类型转换
类型转换是一种将一种数据类型转换为另一种数据类型的方法。一、运算符的类型typeof运算符可以帮助你找到你的变量的类型。typeof运算符返回一个变量或表达式的类型。例:项目JavaScripttypeof运算符typeof运算符返回变量或表达式的类型:document.write(typeof""""typeof"Json"""
Souleigh ✨ Souleigh ✨
3年前
JS - typeof 与 instanceof
一、typeoftypeof操作符返回一个字符串,表示未经计算的操作数的类型使用方法如下:typeof operandtypeof(operand)operand表示对象或原始值的表达式,其类型将被返回举个例子typeof 1 // 'number'typeof '1' // 'string'
Jacquelyn38 Jacquelyn38
3年前
面试官:JavaScript的数据类型你了解多少?
前言作为JavaScript的入门知识点,Js数据类型在整个JavaScript的学习过程中其实尤为重要。最常见的是边界数据类型条件判断问题。我们将通过这几个方面来了解数据类型:概念检测方法转换方法概念undefined、Null、Boolean、String、Number、Symbol、BigInt为基础类型;Ob
明月 明月
1年前
JS数据类型的几种判断方法
1.typeof(常用)typeof是一个运算符,返回值是一个字符串,用来说明变量的数据类型,可以用来判断number,string,object,boolean,function,undefined,symbol这七种类型.2.instanceof(知道即可)instanceof运算符用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上,返回值为布尔值,用于指示一个变量是否属于某个对象的实例3.Object.prototype.to
Stella981 Stella981
2年前
JS中 typeof,instanceof类型检测方式
在js中的类型检测目前我所知道的是三种方式,分别有它们的应用场景:1、typeof:主要用于检测基本类型.typeofundefined;//undefinedtypeof'a';//stringtypeof1;//numbertypeoftrue;//booleanty
Stella981 Stella981
2年前
Js中polyfill的使用限制
随着浏览器的更新,JS也在新增功能功能。这些功能为我们开发带来了很大的便利。在旧浏览器上使用,需要引入相关的polyfill才能用。然而,有些功能在旧浏览器中是无法完美实现的,只有在一定条件下才能使用。本文将为大家分析这些功能的使用限制。Symbol1.禁止用typeof和instanceof,如果要判断,建议判断是否是string。
Stella981 Stella981
2年前
Jenkins UX SIG Meeting
JenkinsUXSIGMeeting展开function\_typeof(e){returne&&"undefined"!typeofSymbol&&e.constructorSymbol?"symbol":typeofe;}!function(e){if("object"
Stella981 Stella981
2年前
Python怎样查询MySQL数据库?
ShowMorefunction\_typeof(e){returne&&"undefined"!typeofSymbol&&e.constructorSymbol?"symbol":typeofe;}!function(e){if("object"("undefined"typeof
Easter79 Easter79
2年前
SpringBoot2 敏捷开发系统
SpringBoot2敏捷开发系统展开function\_typeof(e){returne&&"undefined"!typeofSymbol&&e.constructorSymbol?"symbol":typeofe;}!function(e){if("object"("und
Stella981 Stella981
2年前
Python竟然能画这么漂亮的折线图运营妹子看到直接哭了
ShowMorefunction\_typeof(e){returne&&"undefined"!typeofSymbol&&e.constructorSymbol?"symbol":typeofe;}!function(e){if("object"("undefined"typeof