zoukankan      html  css  js  c++  java
  • 浅克隆,深克隆,防抖,节流

     function clone(data) {
        if (Object.prototype.toString.call(data) === '[object Array]') {
          return [...data]
        } else if (Object.prototype.toString.call(data) === '[object Object]') {
          return { ...data }
        }
        return data
      }

      function deelClone(data) {
        let ws = new WeakSet()
        function clone(data) {
          if (Object.prototype.toString.call(data) === '[object Array]') {
            if (ws.has(data)) return data
            ws.add(data)
            return data.map(item => clone(item))
          } else if (Object.prototype.toString.call(data) === '[object Object]') {
            if (ws.has(data)) return data
            ws.add(data)
            let obj = {}
            for (var i of Object.keys(data)) {
              obj[i] = clone(data[i])
            }
            return obj
          }
          return data
        }
        return clone(data)
      }

      function myInstanceof(obj, proto) {
        let parentProto = Object.getPrototypeOf(obj)
        while (parentProto) {
          if (parentProto === proto.prototype) return true
          parentProto = Object.getPrototypeOf(parentProto)
        }
        return false;
      }

      function debounce(fn, wait, longTime) {
        let time = null, timer = null
        return function () {
          let nowTime = Date.now(), that = this, args = arguments;
          if (!time) time = nowTime
          let runTime = time + longTime - nowTime
          runTime = Math.min(runTime, wait)
          clearTimeout(timer)
          timer = setTimeout(() => {
            timer = time = null
            fn.apply(that, arguments)
          }, runTime)
        }
      }

      function throttle(fn, wait) {
        let timer = null
        return function () {
          if (timer) return
          let that = this, args = arguments;
          timer = setTimeout(() => {
            timer = null
            fn.apply(that, arguments)
          }, runTime)
        }
      }
     
  • 相关阅读:
    [ ERROR ] Error in test library 'pymysql': Creating keyword 'Connect' failed: Keyword with same name defined multiple times.
    【robotframework】pycharm+robotframe(转)
    Django 配置mysql遇到问题(一)
    Django 初始化数据库遇到问题(python manage.py migrate)
    连接mysql报"ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server"
    性能测试-MySQL性能查看(转)
    APP安全测试
    在线java堆栈分析工具
    性能-如何根据线程和进程找到性能瓶颈或者问题点
    Jmeter CSV参数带汉字处理
  • 原文地址:https://www.cnblogs.com/gudun/p/14709231.html
Copyright © 2011-2022 走看看