lodash中判断数据类型的⽅法lodash库中判断数据类型涉及两个⽅法,getTag和baseGetTag,下⾯是baseGetTag:
const objectProto = Object.prototype
const hasOwnProperty = objectProto.hasOwnProperty//Object.prototype.hasOwnProperty
const toString = String//String
const symToStringTag = typeof Symbol != 'undefined' ? StringTag : undefined
//当前环境是否⽀持Symbol然后获取对应的StringTag
/**
* The base implementation of `getTag` without fallbacks for buggy environments.
*
* @private
* @param {*} value The value to query.
* @returns {string} Returns the `toStringTag`.
*/
function baseGetTag(value) {
if (value == null) {//如果值是undefined和null返回对应tag
return value === undefined ? '[object Undefined]' : '[object Null]'
}
if (!(symToStringTag && symToStringTag in Object(value))) {
//如果不⽀持Symbol或者value值上⾯没有StringTag属性,直接返回String调⽤后的值return toString.call(value)
}
const isOwn = hasOwnProperty.call(value, symToStringTag)//value⾃⾝是否有StringTag属性
const tag = value[symToStringTag]//存下value上的StringTag属性
let unmasked = false//将value⾃⾝上的StringTag属性重置为undefined是否成功
try {//尝试将value⾃⾝上的StringTag属性重置为空
value[symToStringTag] = undefined
unmasked = true
} catch (e) {}
const result = toString.call(value)
//value⾃⾝StringTag属性置空后获取toString处理后的tag
if (unmasked) {//如果StringTag属性置空成功,再将StringTag属性变回原样
if (isOwn) {
value[symToStringTag] = tag
} else {
delete value[symToStringTag]
}
}
return result
}
export default baseGetTag
下⾯是getTag:
import baseGetTag from './baseGetTag.js'
/** `Object#toString` result references. */
//Dateview,Map,Promise,Set,WeakMap这⼏种类型和Object应该返回的类型tag
const dataViewTag = '[object DataView]'
const mapTag = '[object Map]'
const objectTag = '[object Object]'
const promiseTag = '[object Promise]'
const setTag = '[object Set]'
const weakMapTag = '[object WeakMap]'
/** Used to detect maps, sets, and weakmaps. */
//Dateview,Map,Promise,Set,WeakMap这⼏种类型的构造函数属性返回的字符串
const dataViewCtorString = `${DataView}`
const mapCtorString = `${Map}`
const promiseCtorString = `${Promise}`
const setCtorString = `${Set}`
const weakMapCtorString = `${WeakMap}`
/**
* Gets the `toStringTag` of `value`.
*
* @private
* @param {*} value The value to query.
* @returns {string} Returns the `toStringTag`.
*/
let getTag = baseGetTag
// Fallback for data views, maps, sets, and weak maps in IE 11 and promises in Node.js < 6.
if ((DataView && getTag(new DataView(new ArrayBuffer(1))) != dataViewTag) ||
(getTag(new Map) != mapTag) ||
(solve()) != promiseTag) ||
(getTag(new Set) != setTag) ||
(getTag(new WeakMap) != weakMapTag)) {//如果在某些环境下⽆法判断这5种类型,就做⼀下兼容处理  getTag = (value) => {
const result = baseGetTag(value)
const Ctor = result == objectTag ? structor : undefined
const ctorString = Ctor ? `${Ctor}` : ''
//获取值的constructor构造函数属性,如果获取到了就返回对应的tag,如果获取不到就返回原始toString值if (ctorString) {
switch (ctorString) {
case dataViewCtorString: return dataViewTag
case mapCtorString: return mapTag
case promiseCtorString: return promiseTag
case setCtorString: return setTag
case weakMapCtorString: return weakMapTag
}
}
return resultlodash有哪些方法
}
}
export default getTag

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。