zoukankan      html  css  js  c++  java
  • js 柯里化、深拷贝、浅拷贝

    curry

    const sum = (a, b, c, d) => a + b + c + d
    const curry = fn =>
      (judge = (...args) =>
        args.length >= fn.length ? fn(...args) : (...arg) => judge(...args, ...arg))
    const currySum = curry(sum)
    console.log(currySum(1, 2)(4)(3)) //10
    

    deepClone

    function getType(obj) {
      const str = Reflect.toString.call(obj)
      const map = {
        '[object Boolean]': 'boolean',
        '[object Number]': 'number',
        '[object String]': 'string',
        '[object Function]': 'function',
        '[object Array]': 'array',
        '[object Date]': 'date',
        '[object RegExp]': 'regExp',
        '[object Undefined]': 'undefined',
        '[object Null]': 'null',
        '[object Object]': 'object',
        '[object Map]': 'map',
        '[object Set]': 'set',
      }
      if (obj instanceof Element) {
        // 判断是否是dom元素,如div等
        return 'element'
      }
      return map[str]
    }
    
    function deepClone(ori) {
      const type = getType(ori)
      let copy
      switch (type) {
        case 'array':
          return copyArray(ori, copy)
        case 'object':
          return copyObject(ori, copy)
        case 'function':
          return copyFunction(ori)
        case 'map':
          return copyMap(ori, copy)
        case 'set':
          return copySet(ori, copy)
        default:
          return ori
      }
    }
    
    function copyArray(ori, copy = []) {
      for (const [index, value] of ori.entries()) {
        copy[index] = deepClone(value)
      }
      return copy
    }
    
    function copyObject(ori, copy = {}) {
      for (const [key, value] of Reflect.entries(ori)) {
        copy[key] = deepClone(value)
      }
      return copy
    }
    
    function copyMap(ori, copy = new Map()) {
      for (const [key, value] of ori) {
        copy.set(deepClone(value))
      }
      return copy
    }
    
    function copySet(ori, copy = new Set()) {
      for (const [key, value] of ori.entries()) {
        copy.add(key, deepClone(value))
      }
      return copy
    }
    
    function copyFunction(ori) {
      const fun = eval(ori.toString())
      fun.prototype = ori.prototype
      return fun
    }
    
    

    shallowClone

    function shallowClone(ori) {
      if (typeof ori === 'object' && ori !== null) {
        return Array.isArray(ori) ? copyArray(ori) : copyObject(ori)
      }
      return ori
    }
    function copyObject(ori, copy = {}) {
      for (const [k, v] of Reflect.entries(ori)) {
        copy[k] = v
      }
      return copy
    }
    function copyArray(ori, copy = []) {
      for (const [k, v] of ori.entries()) {
        copy[k] = v
      }
      return copy
    }
    
    
  • 相关阅读:
    清除浮动的几种方式
    css 居中问题总结
    Python 数据库Insert语句脚本生成工具(SQL Server)
    Windows安装运行Kafka
    C# 阿里云视频点播--视频转码
    C# 阿里云视频点播
    C# Assembly.LoadFile [A] 无法强制转换为 [B]
    OssFtp 用法
    C# Aspose.Words 用法
    C# 企业微信消息推送对接
  • 原文地址:https://www.cnblogs.com/smzd/p/11912827.html
Copyright © 2011-2022 走看看