You're never really done for, as long as you‘ve got a good story and someone to tell it to.

手写JS函数

我这里手写一下常见的面试题

深拷贝

function deepcopy(val){

  const getType = value => Object.prototype.toString.call(value).slice(8,-1);
  const constructor = getType(val);
  if( constructor !== 'Array' && constructor !== 'Object') return val;
  const newObj = constructor === 'Array' ? [] : {};
  for(let i in val){
    const newType = getType(val[i]);
    newObj[i] = newType === 'Array' || newType === 'Object' ? deepcopy(val[i]) : val[i];
  }
  return newVal;
}

promise

class Prom {
  constructor(fn){
    this.status = 'PENDING';
    this.value = undefined;
    this.reason = undefined;
    this.resolveFns = [];
    this.rejectFns = [];

    const resolve = value => {
      this.status = 'RESOLVED';
      this.value = value;
      this.resolveFns.forEach(({fn, resolve: res, reject: rej})=>res(fn(value)))
    }

    const reject = e => {
      this.status = 'REJECTED';
      this.reason = e;
      this.resolveFns.forEach(({fn, resolve: res, reject: rej})=>rej(fn(e)))
    }
    fn(resolve, reject);
  }

  static resolve(value){
    if(value && value.then){
      return value;
    }
    return new Prom(resolve=>resolve(value))
  }

  then(fn){
    if(this.status === 'RESOLVED'){
      return Prom.resolve(fn(this.value));
    }

    if(this.status === 'PENDING'){
      return new Prom((resolve, reject) => {
        this.resolveFns.push({fn, resolve, reject});
      })
    }
  }


  catch(fn){
    if(this.status === 'REJECTED'){
      return Prom.resolve(fn(this.reason));
    }
    if(this.status === 'PENDING'){
      return new Prom((resolve, reject) => {
        this.rejectFns.push({fn, resolve, reject});
      })
    }  
  }


  static all(iterators){
    const promises = Array.from(iterators);
    const len = promises.length;
    let count = 0;
    let results = [];
    return new Prom((resolve, reject)=>{
      promises.forEach((p,indnx)=>{
        Prom.resolve(p).then(result=>{
          count++;
          results[index] = result;
          if(index === count) resolve(results);
        }).catch(e=>reject(e))
      })
    })
  }
}

new

function _new() {
  const Constructor = [].shift.call(arguments);
  const obj = Object.create(Constructor.prototype);
  const res = Constructor.apply(obj, arguments);
  return typeof res === 'object' ? res : obj;
}

debounce

function debounce(fn, wait){
  let timer = null;
  return function(){
    const _this = this;
    const args = arguments;
    if(timer) clearTimeout(timer)
    timer = setTimeout(() => fn.apply(_this, args) , wait)
  }
}

throttle

function throttle(fn, wait){
  let timer = null
  return function(){
    const _this = this
    const args = arguments
    if(timer) return;
    timer = setTimeout(() => fn.apply(_this, args), wait)
  }
}

instanceof

function myInstanceof(left, right){
  // 获取类型的原型
  let prototype = right.prototype;
  // 获得对象原型
  left = left.__proto__;
  // 判断对象的类型是否等于类型的原型
  while(left){
    if(prototype === left)
    return true;
    left = left.__proto__;
  }
  return false
}
# 面试   javascript  

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×