如何加强企业网站建设论文,深圳市建设银行网站,拍宣传片比较好的公司,seo关键词优化怎么做众所周知js内置的类型检测机制不可靠#xff0c;比如typeof操作符#xff0c;对于正则和数组检测时返回值都是object#xff0c; 而使用instanceof检测类型时#xff0c;虽然可以对正则和数组正常验证#xff0c;但验证undefined会报错#xff0c;还有对于Symbol无法验证…众所周知js内置的类型检测机制不可靠比如typeof操作符对于正则和数组检测时返回值都是object 而使用instanceof检测类型时虽然可以对正则和数组正常验证但验证undefined会报错还有对于Symbol无法验证
Object.prototype.toString执行的时候会返回当前方法中this的所属信息。也就是说但我们需要检测类型时就把这个toString执行并其让this变为我们检测的这个数据值那么返回的结果就是当前检测这个值的类型
/* 安全的类型检测*/
const typeChecking {}
typeChecking.isArray function(value) {return Object.prototype.toString.call(value) [object Array]
}
typeChecking.isFunction function(value) {return Object.prototype.toString.call(value) [object Function]
}
typeChecking.isNumber function(value) {return Object.prototype.toString.call(value) [object Number]
}
typeChecking.isBoolean function(value) {return Object.prototype.toString.call(value) [object Boolean]
}
typeChecking.isString function(value) {return Object.prototype.toString.call(value) [object String]
}
typeChecking.isRegExp function(value) {return Object.prototype.toString.call(value) [object RegExp]
}
typeChecking.isUndefined function(value) {return Object.prototype.toString.call(value) [object Undefined]
}
typeChecking.isSymbol function(value) {return Object.prototype.toString.call(value) [object Symbol]
}