前端基础手撕题

深拷贝实现方式 

function deepCopy(oldObj, newobj) {
    for (var key in oldObj) {
        var item = oldObj[key];
        
        if (item instanceof Object) {
            if (item instanceof Function) {
                newobj[key] = oldObj[key];
            } else {
                newobj[key] = {};  
                deepCopy(item, newobj[key]); 
            }

            
        } else if (item instanceof Array) {
            newobj[key] = [];  
            deepCopy(item, newobj[key]); 
        } else {
            newobj[key] = oldObj[key];
        }
    }
}

封装通用柯里化函数 

function curry() {
    var fn = arguments[0]; 
    var args = [].slice.call(arguments, 1); 
    
    if (args.length === fn.length) {
        return fn.apply(this, args)
    }
    
    function _curry(){
        
        
        args.push(...arguments);
        if(args.length === fn.length){
            return fn.apply(this, args)
        }
        return _curry;
    }
    return _curry;
}

function add(a, b, c) {
    return a + b + c;
}

console.log(curry(add)(1)(2)(3)); 

冒泡排序 

function bubbleSort(arr) {
    const len = arr.length
    for (let i = 0
        for (let j = 0
            if (arr[j] > arr[j+1]) {        // 相邻元素两两对比
                var temp = arr[j+1]
                arr[j+1] = arr[j]
                arr[j] = temp
            }
        }
    }
    return arr
}

函数防抖 

 * 函数防抖
 * @param {function} func 一段时间后,要调用的函数
 * @param {number} wait 等待的时间,单位毫秒
 */
function debounce(func, wait){
    
    let timerId = null;
    return function(...args){
        if(timerId){
            
            clearTimeout(timerId);
        }
        
        timerId = setTimeout(() => {
            func(...args);
        }, wait);
    }
}


函数节流 

 * 
 * @param {要节流执行的函数} func 
 * @param {节流的时间间隔} wait 
 * @returns 
 */
function throttle(func, wait) {
    
    
    var timeout, args;
    return function () {
        args = arguments;
        
        if (!timeout) {
            
            
            func.apply(null, args)
            
            timeout = setTimeout(function () {
                timeout = null;
            }, wait);
        }
    }
}

实现数组去重 

ES6方法(使用数据结构集合):

const array = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8];

Array.from(new Set(array)); 
复制代码

数组扁平化 

ES5方法:使用map存储不重复的数字

const array = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8]

uniqueArray(array)

function uniqueArray(array) {
  let map = {}
  let res = []
  for(var i = 0
    if(!map.hasOwnProperty([array[i]])) {
      map[array[i]] = 1
      res.push(array[i])
    }
  }
  return res
}

ES5方法

function flatten(arr) {
      return arr.reduce((result, item) => {
        return result.concat(Array.isArray(item) ? flatten(item) : item);
      }, []);
}


ES6方法

let arr = [1, [2, [3, 4]]]; 
function flatten(arr) { return arr.flat(Infinity); } 
console.log(flatten(arr));

Promise.all 

Promise.myAll = function(promiseArr) {
  return new Promise((resolve, reject) => {
    const ans = [];
    let index = 0;
    for (let i = 0; i < promiseArr.length; i++) {
      promiseArr[i]
      .then(res => {
        ans[i] = res;
        index++;
        if (index === promiseArr.length) {
          resolve(ans);
        }
      })
      .catch(err => reject(err));
    }
  })
}

猜你喜欢

转载自blog.csdn.net/Likestarr/article/details/135415654