js中常用的变量类型检测

检查变量类型

1、检查是否为数组

const arr = []
if( Array.isArray(arr) ) console.log('arr是数组')

2、检查是否为方法类型

function testFun() {
}
if(typeof testFun === 'function') consloe.log('testFun为一个方法')

3、检测一个变量是否为特定类

function Person() {
	console.log('Im person!')
}
const tom = new Person
if(tom instanceof Person) console.log('tom 是一Person对象')
发布了132 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/youlinhuanyan/article/details/104375946
今日推荐