js怎样判断对象的数据类型
这篇文章主要介绍js怎样判断对象的数据类型,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

成都创新互联专业为企业提供临城网站建设、临城做网站、临城网站设计、临城网站制作等企业网站建设、网页设计与制作、临城企业网站模板建站服务,十多年临城做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
判断对象的数据类型
使用Object.prototype.toString配合闭包来实现对象数据类型的判断:
const isType = type => target => `[object ${type}]` === Object.prototype.toString.call(target)
const isArray = isType('Array')([1, 2, 3])
console.log(isArray)
> Result: true上面的代码相当于:
function isType(type){
return function (target) {
return `[object ${type}]` === Object.prototype.toString.call(target)
}
}
isType('Array')([1,2,3])
> Result: true或者:
const isType = type => target => `[object ${type}]` === Object.prototype.toString.call(target)
const isString = isType('String')
const res = isString(('1'))
console.log(res)
> Result: true以上是“js怎样判断对象的数据类型”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
新闻名称:js怎样判断对象的数据类型
当前链接:http://lzwzjz.cn/article/pepssh.html


咨询
建站咨询
