首页 > 其他 > 详细

类型判断

时间:2020-07-21 15:42:22      阅读:53      评论:0      收藏:0      [点我收藏+]

判断核心使用Object.prototype.toString,这种方式可以准确的判断数据类型。

/** * @param {any} target * @param {string} type * @return {boolean} */function isType(target, type) {  let targetType = Object.prototype.toString.call(target).slice(8, -1).toLowerCase()  return targetType === type.toLowerCase()}

使用

isType([], ‘Array‘) // trueisType(/\d/, ‘RegExp‘) // trueisType(new Date(), ‘Date‘) // trueisType(function(){}, ‘Function‘) // trueisType(Symbol(1), ‘Symbol‘) // true

类型判断

原文:https://www.cnblogs.com/blhgys/p/13354324.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!