携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第9天,点击查看活动详情
题目: 写一个函数 isEmpty ,判断空对象、集合、映射或者set
检查 value 是否为一个空对象,集合,映射或者set。 判断的依据是除非是有枚举属性的对象,length 大于 0 的 arguments object, array, string 或类jquery选择器。
对象如果被认为为空,那么他们没有自己的可枚举属性的对象。
类数组值,比如arguments对象,array,buffer,string的length 为 0,被认为是空。类似的,map(映射)和set 的size 为 0,被认为是空。
例子
_.isEmpty(null);
// => true
_.isEmpty(true);
// => true
_.isEmpty(1);
// => true
_.isEmpty([1, 2, 3]);
// => false
_.isEmpty({ 'a': 1 });
// => false
我们先来看看题目,需要判断Object、Map、Set是否为空对象,而null、String、Number、Boolean都判断为空对象。
先来看看lodash是怎么写的
import getTag from './.internal/getTag.js';
import isArguments from './isArguments.js';
import isArrayLike from './isArrayLike.js';
import isBuffer from './isBuffer.js';
import isPrototype from './.internal/isPrototype.js';
import isTypedArray from './isTypedArray.js';
/** Used to check objects for own properties. */
const hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* Checks if `value` is an empty object, collection, map, or set.
*
* Objects are considered empty if they have no own enumerable string keyed
* properties.
*
* Array-like values such as `arguments` objects, arrays, buffers, strings, or
* jQuery-like collections are considered empty if they have a `length` of `0`.
* Similarly, maps and sets are considered empty if they have a `size` of `0`.
*
* @since 0.1.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is empty, else `false`.
* @example
*
* isEmpty(null)
* // => true
*
* isEmpty(true)
* // => true
*
* isEmpty(1)
* // => true
*
* isEmpty([1, 2, 3])
* // => false
*
* isEmpty('abc')
* // => false
*
* isEmpty({ 'a': 1 })
* // => false
*/
function isEmpty(value) {
if (value == null) {
return true;
}
if (
isArrayLike(value) &&
(Array.isArray(value) ||
typeof value === 'string' ||
typeof value.splice === 'function' ||
isBuffer(value) ||
isTypedArray(value) ||
isArguments(value))
) {
return !value.length;
}
const tag = getTag(value);
if (tag == '[object Map]' || tag == '[object Set]') {
return !value.size;
}
if (isPrototype(value)) {
return !Object.keys(value).length;
}
for (const key in value) {
if (hasOwnProperty.call(value, key)) {
return false;
}
}
return true;
}
export default isEmpty;
进入函数,首先判断null,如果是null直接返回true
if (value == null)
再判断是否类数组或者就是一个数组
if (
isArrayLike(value) &&
(Array.isArray(value) ||
typeof value === 'string' ||
typeof value.splice === 'function' ||
isBuffer(value) ||
isTypedArray(value) ||
isArguments(value))
)
而isArrayLike其实是这样一个函数
import isLength from './isLength.js'
/**
* Checks if `value` is array-like. A value is considered array-like if it's
* not a function and has a `value.length` that's an integer greater than or
* equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
*
* @since 4.0.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is array-like, else `false`.
* @example
*
* isArrayLike([1, 2, 3])
* // => true
*
* isArrayLike(document.body.children)
* // => true
*
* isArrayLike('abc')
* // => true
*
* isArrayLike(Function)
* // => false
*/
function isArrayLike(value) {
return value != null && typeof value !== 'function' && isLength(value.length)
}
export default isArrayLike
其中核心就是这句
value != null && typeof value !== 'function' && isLength(value.length)
value不能是null,并且value不是一个function,并且value的length属性大于等于0(length具体在./isLength.js里面判断)
再判断是不是Map、Set
if (tag == '[object Map]' || tag == '[object Set]')
我们来看看tag是什么
const toString = Object.prototype.toString
/**
* Gets the `toStringTag` of `value`.
*
* @private
* @param {*} value The value to query.
* @returns {string} Returns the `toStringTag`.
*/
function getTag(value) {
if (value == null) {
return value === undefined ? '[object Undefined]' : '[object Null]'
}
return toString.call(value)
}
export default getTag
只是封装的一个toString方法。用来判断传入的值是object Map或者object Set
然后判断是不是原型对象
if (isPrototype(value))
最后再遍历他自己(对象)
for (const key in value)