lodash常用函数

深拷贝

deepClone(source) {
		if (!source && typeof source !== 'object') {
			throw new Error('error arguments', 'shallowClone')
		}
		const targetObj = source.constructor === Array ? [] : {}
		for (const keys in source) {
			if (source.hasOwnProperty(keys)) {
				if (source[keys] && typeof source[keys] === 'object') {
					targetObj[keys] = source[keys].constructor === Array ? [] : {}
					targetObj[keys] = this.deepClone(source[keys])
				} else {
					targetObj[keys] = source[keys]
				}
			}
		}
		return targetObj
	}

防抖

debounce(fn, delay) {
		let timer;
		return ()=>{
			if(timer){
				clearTimeout(timer)
				timer=null
			}
			timer=setTimeout((...args)=>{
				fn.call(this,args)
			},delay)
		}
	}

日期

//y-m-d
	getDate() {
		const date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
		month = month.toString().padStart(2, '0')
		day = day.toString().padStart(2, '0')
		return `${year}-${month}-${day}`;
	}
  //h-m-s
	getTime() {
		const date = new Date();
		let hour = date.getHours().toString().padStart(2, '0');
		let minute = date.getMinutes().toString().padStart(2, '0');
		let second = date.getSeconds().toString().padStart(2, '0');
		return `${hour}:${minute}:${second}`;
	}

判断是否是JSON字符串

	isJSONStr(str){
			try{
				if(JSON.parse(str) instanceof Object){
					return true
				}
			}
			catch(error){
				console.log(error)
				return false
			}
	}

判断空类型

//判断是否是空对象{}
	isEmptyObj(obj) {
    return obj !== null && typeof obj === 'object' && !Array.isArray(obj)&& (Object.keys(obj).length === 0)
  }
	//是否是空数组[]
	isEmptyArr(Arr){
		return Array.isArray(Arr)&&Arr.length===0
	}
	//是否是空,undefined,null,"",{},[]
	isEmpty(value){
		if(!value&&value!==0){
			return true
		}
    else{
			if(Object.prototype.toString.call(value)==="[object Array]"){
				return value.length===0
			}
			if(Object.prototype.toString.call(value)==="[object Object]"){
				return Object.keys(value).length === 0
			}
		}
		return false
	}

你可能感兴趣的:(JS基础)