手写系列

106 阅读1分钟

防抖节流

防抖函数

// debounce
function debounce(func, wait) {
  let timer = null;
  return function(){
    let context = this;
    let args = arguments;
    if(timer) clearTimeout(timer);
    timer = setTimeout(() => {
      func.apply(context, args);
    }, wait);
  }
}

节流函数

// throttle 函数
function throttle(func, wait){
  let begin = 0;
  return function(){
    let context = this;
    let args = arguments;
    let cur = new Date().getTime();
    if(cur - begin > wait) {
      func.apply(context, args);
      begin = cur;
    }
  }
}

深拷贝

// deepClone
function deepClone(obj){
  if(typeof obj !== 'object' || obj == null) {
    return obj;
  }

  let result;
  // 判断是数组还是对象
  if(obj instanceof Array){
    result = [];
  }else{
    result = {};
  }

  for(let key in obj){
    if(obj.hasOwnProperty(key)){
      result[key] = deepClone(obj[key]);
    }
  };

  return result;

}

深度相等

// 判断是否是对象或数组
function isObject(obj) {
    return typeof obj === 'object' && obj !== null
}

// 全相等(深度)
function isEqual(obj1, obj2){
    if(!isObject(obj1) || !isObject(obj2)){
        // 值类型(注意,参与 equal 的一般不会是函数)
        return obj1 === obj2
    }
    
    if (obj1 === obj2) {
        return true
    }
    
     // 两个都是对象或数组,而且不相等
    // 1. 先取出 obj1 和 obj2 的 keys ,比较个数
    const obj1Keys = Object.keys(obj1)
    const obj2Keys = Object.keys(obj2)
    if (obj1Keys.length !== obj2Keys.length) {
        return false
    }
    
    // 2. 以 obj1 为基准,和 obj2 一次递归比较
    for (let key in obj1) {
        // 比较当前 key 的 val —— 递归!!!
        const res = isEqual(obj1[key], obj2[key])
        if (!res) {
            return false
        }
    }
    
    // 3. 全相等
    return true
}