2020-12-03 滴~ 滴水

1.JavaScript的语言类型:

基本数据类型
     Number、Boolean、 String、 undefined 、 null、 Symbol
引用类型
      Object
  1. 防抖函数
    在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时。
  function debounce (fn, wait, immediate) {
       let timeout = null;
       return (...args) => {
            timeout && clearTimeout(timeout);
            if (immediate) {
                fn.apply(this, args);
                immediate = false;
            } else {
                 timeout = setTimeout(() => fn.apply(this, args), wait);
            }
      }
  }
  1. 节流函数
    规定在一个单位时间内,只能触发一次函数。如果这个单位时间内触发多次函数,只有一次生效。
    function throttle (fn, delay)  {
          let flag = true;
          return (...args) => {
              if (!flag) return;
              flag = false;
              setTimeout( () => {
                fn.apply(this, args);
                flag = true;
              }, delay);
          }
    }
  1. 深拷贝
    function deepCopy (obj) {
      const isType = (item, type) => {
        if (typeof item !== 'object') return false;
        const typeString = Object.prototype.toString.call(item).slice(8, -1);
        return typeString === type;
      }
      const getRegExp = re => {
            let item = "";
            re.global && (item += 'g');
            re.ignoreCase && (item += 'i');
            re.mutiline && (item += 'm');
            return item;
      }

      // 维护两个储存循环引用的数组
          const parents = [];
          const children = [];

      const  copy = obj => {
            if (obj === null) return null;
            if (typeof obj !== 'object') return obj;
            let child, proto;
            if (isType(obj, 'Array')) {
                child = [];
            } else if (isType(obj, 'RegEpx')) {
                 child = new RegEpx(obj.source, getRegExp(obj));
                  obj.lastIndex && (child.lastIndex = obj.lastIndex);
            } else if (isType(obj, 'Date')) {
                child = new Date(obj.getTime());
            } else {
                 proto = Object.getPrototypeOf(obj);
                 child = Object.create(proto);
            }
            const index = parents.indexOf(obj);
            if (index !== -1) {
                  return children[index];
            }
            parents.push(obj);
            children.push(child);
            for (let i in obj) {
              // 递归
              child[i] = copy(obj[i]);
            }
            return child;
      }
      return copy(obj);
  }
    //简略版本(只考虑对象、数组)


deepClone(obj) {
  const isType = (item, type) => {
    const typeString = Object.prototype.toString.call(item).slice(8, -1);
    return typeString === type
  };
  let result;
  if (isType(obj, 'Object')) {
     result = {};
  } else if (isType(obj, 'Array')) {
    result = []
  } else {
      return obj
  }
  for (let i in obj) {
    let value = obj[i];
    if (isType(value, 'Object') || isType(value, 'Array')) {
        result[i] = deepCopy(value);
    } else {
        result[i] = value
    }
  }
  return result;
}
  1. event-bus
Class EventEmeitter () {
  constructor() {
    this._event = this._event || new Map();
    this._maxListeners = this._maxListeners || 10;
  }
  emeit(type, ...args) {
     let handler;
     handler = this._event.get(type);
     if (args.length > 0) {
       handler.apply(this, args);  
     } else {
       handler.call(this); 
     }
     return true;
  }
  on (type, fn) {
     if (!this._event.get(type)) {
      this._event.set(type, fn);
    }
  }
}
  1. instanceOf
function instanceOf (source, target) {
  target = target.prototype;
  source = source.__proto__;
  while(true) {
    if (source === null) return false;
    if (source === target) {
      return true;
    }
    source = source.__proto__;
  }
}            
  1. new
function ourNew(context) {  
    // 1.创建一个空对象
    var person = new Object();
    // 2.取出构造函数
    var constructor = [].shift.call(arguments);
    // 3.继承构造函数的原型
    person._proto_ = constructor.prototype;
    // 4.为新建的对象调用构造函数,生成内部属性
    constructor.apply(person, arguments);
    // 5.返回对象
    return person;
}


 








你可能感兴趣的:(2020-12-03 滴~ 滴水)