js手写题总结part2

130 阅读1分钟
  • 深拷贝
function deepclone(oldobj){
  let newobj={};
  for(let item in oldobj){
    if(oldobj[item] instanceof Obj){
      newobj[item]=deepclone(oldobj)
    }else{
      newobj[item]=oldobj[item]
    }
  }
  return newobj;
}
  • 手写instanceof
function myinstanceof(child,parent){
  while(child.__proto__!=null){
    if(child.__proto__==parent.prototype){
      return true
    }else{
      child=child.__proto__
    }
  }
  return false;
}
  • 柯里化
function curry(fn,...args){ 
  let len=fn.length;
  let allargs=[...args]
  const res=function(newargs){
    allargs=[...newargs,...allargs];
    if(allargs.length==len){
      return fn(allargs) 
    }else{ 
      return res
    }
  }
  return res;
}
  • 冒泡排序
function bubblesort(arr){
  for(let i=0;i<arr.length-1;i++){
    for(let j=0;j<arr.length-1-i;j++){
      if(arr[j]>arr[j+1]{
        let item=arr[j];
        arr[j]=arr[j+1];
        arr[j+1]=item;
      }
    }
  }
  return arr;
}
  • 选择排序
function selectsort(arr){
  for(let i=0;i<arr.length;i++){
    let minindex=i;
    for(let j=i+1;j<arr.length;j++){
      if(arr[j]<arr[minindex]){
        minindex=j;
      }
    }
    if(minindex!=i){
      [arr[minindex],arr[i]]=[arr[i],arr[minindex]]
    }
  }
}
  • 插入排序
function insertsort(arr){
  for(let i=i;i<arr.length;i++){
    let j=i;
    let target=arr[i]
    while(arr[j]>target){
        arr[j]=arr[j-1];
        j--;
    }
    arr[j]==target;
  }
}
  • 快速排序
  function quicksort(left, right, arr) {
    if (left >= right) {
      return arr
    }
    let index = left;
    let i = left;
    let j = right;
    while (i < j) {
      while (arr[i] < arr[index] && i < j) {
        i++;
      }
      while (arr[j] > arr[index] && i < j) {
        j--;
      }
      [arr[i], arr[j]] = [arr[j], arr[i]]
    }
    [arr[i], arr[index]] = [arr[index], arr[i]]
    quicksort(left, i - 1, arr);
    quicksort(i + 1, right, arr);
    return arr;
  }
  • 归并排序
 function mergesort(arr) {
    if (ar.length <= 1) {
      return arr
    }
    let mid = arr.length / 2
    let left = arr.slice(0, mid)
    let right = arr.slice(mid + 1, arr.length)
    merge(mergesort(left), mergesort(right))
  }
  function merge(left, right) {
    let res = [];
    let i = 0;
    let j = 0;
    while (i < left.length && j < right.length) {
      if (left[i] > right[j]) {
        res.push(right[j])
        j++;
      }
      if (right[j] > left[i]) {
        res.push(left[i])
        i++;
      }
    }
    while (i < left.length) {
      res.push(left[i])
    }
    while (j < right.length) {
      res.push(right[j])
    }
    return res
  }
  • 二分查找
  function halffind(arr, target, start, end) {
    let targetindex = -1;
    let mid = (start + end) / 2
    if (start < end) {
      return targetindex
    }
    if (arr[mid] == target) {
      targetindex = mid
      return targetindex
    }
    if (arr[mid] < target) {
      halffind(arr, target, start, mid)
    } else {
      halffind(arr, target, mid, end)
    }
  }
  • 防抖节流
function debounce(fn,time){
  let timer=null;
  return function(){
    if(timer){
      cleartimeout(timer);
    }
    timer=settimeout(fn,time)
  }
}
function throttle(fn,time){
 let flag=true;
 return function(...args){
   if(flag){
     settimeout(()=>{
       fn();
       flag=true;
     },time)
   }
   flag=false;
 }
}
  • 手写promise和then 还有race和all
  class mypromise {
    constructor(fn) {
      this.status = 'pending';
      this.resolvecallback = [];
      this.rejectcallback = [];
      this.value = null;
      const resolve = function (value) {
        if (this.status != 'pending') {
          return
        }
        this.status = 'resolve'
        this.value = value;
        setTimeout(() => {
          this.resolvecallback.forEach(fn => {
            fn(this, val)
          });
        })
      }
      const reject = function (err) {
        if (this.status != 'pending') {
          return
        }
        this.status = 'reject';
        setTimeout(() => {
          this.rejectcallback.forEach(fn => {
            fn(this, err)
          })
        })
      }
      try {
        fn(resolve, reject)
      } catch {
        reject(err)
      }
    }
  }
mypromise.prototype.then=function(onresolved,onreject){
  // onresolved=Instanceof(onresolved,'Function')?onresolved:function;
  // onreject=Instanceof(onreject,'Function')?onreject:function;
  return new mypromise((resolve,reject)=>{
    this.resolvecallback.push((value)=>{
      let x=onresolved(value)
      x.then(resolve,reject)
    })
  })
}
function all(promises){
  return new promise((resolve,reject)=>{
    let len=promises.length;
    let result=[];
    let rescount=0;
    for(let i=0;i<promises.length;i++){
      promises[i].then((res)=>{
        result.push(res)
        rescount++;
        if(rescount==len){
          resolve(result)
        }
      })
    }
  })
}
function race(promises){
  return new promise((resolve,reject)=>{
    for(let i=0;i<promises.length;i++){
      promises[i].then((res)=>{
        resolve(res)
      })
    }
  })
}
  • 模板字符串替换
  function charreplace(template,data){
    const reg=/\{\{(\w+)\}\}/
    if(reg.exec(template).length){
      let name=reg.exec(template)[1]
      template.replace(template,data[name])
      charreplace(template,data)
    }
  }