/**
* 混合防抖函数
*/
// function debounce(fn, t, imm) {
// let timer = null
// function _debounce() {
// const ctx = this
// console.log(this)
// const args = [...arguments]
// if(timer) clearTimeout(timer)
// if(imm) {
// const isRightNow = !timer
// timer = setTimeout(_ => {
// timer = null
// },t)
// if(isRightNow) fn.apply(ctx, args)
// } else {
// timer = setTimeout(_ => {
// fn.apply(ctx, args)
// }, t)
// }
// }
// _debounce()
// }
// function foo() {
// console.log('foo 已执行')
// }
// debounce(foo, 2000, true)
/**
* 手写bind
*/
// Function.prototype.myBind = function(ctx, ...args) {
// const fn = this
// return function(args2) {
// const nArgs = [...args, ...args2]
// if(new.target) {
// return new fn(...nArgs)
// } else {
// return fn.apply(ctx, [...nArgs])
// }
// }
// }
// function foo() {
// console.log('foo 已执行')
// }
// const newFn = foo.bind(1,2,3)
// newFn()
// const r = new newFn()
/**
* call
*/
// Function.prototype.myCall = function(ctx, ...args) {
// const nCtx = ctx === null || ctx === undefined ? globalThis : Object(ctx)
// const fn = this
// const k = Symbol
// Object.defineProperty(nCtx, k, {
// value: fn,
// enumerable: false
// })
// nCtx[k] = fn
// const result = nCtx[k](...args)
// delete nCtx[k]
// return result
// }
// function foo(a,b) {
// console.log(a,b)
// console.log('this',this)
// }
// foo.myCall({
// fn(){}
// },2,3)
// function deepClone (value) {
// const cache = new Map()
// function _deepClone(val) {
// if(typeof val !== 'object' || val === null) {
// return value
// }
// if(cache.has(val)) {
// return cache.get(val)
// }
// const result = Array.isArray(val) ? [] : {}
// cache.set(val, result)
// for(let v in val) {
// result[v] = _deepClone(val[v])
// }
// return result
// }
// return _deepClone(value)
// }
// const obj = {
// a:[],
// b:{},
// c:1
// }
// obj.a.push(obj)
// obj.b.d = obj.a
// const nObj = deepClone(obj)
// console.log(nObj.a === obj.a)
// console.log(nObj.b.d === obj.a)
// console.log(nObj === obj)
// const timeout = (time) => new Promise(resolve => setTimeout(resolve, time))
// class SuperTask {
// constructor(parallelCount = 2) {
// this.parallelCount = parallelCount
// this.taskList = []
// this.runningCount = 0
// }
// add(task) {
// return new Promise((resolve,reject) => {
// this.taskList.push({
// task,resolve,reject
// })
// this._run()
// })
// }
// _run() {
// while(this.runningCount < this.parallelCount && this.taskList.length) {
// const {task,resolve,reject} = this.taskList.shift()
// this.runningCount ++
// Promise.resolve(task()).then(resolve, reject).finally( _ => {
// this.runningCount --
// this._run()
// })
// }
// }
// }
// const superTask = new SuperTask()
// function addTask(time, name) {
// superTask
// .add(() => timeout(time))
// .then(() => {
// console.log(`任务 ${name} 完成`)
// })
// }
// addTask(10000, 1) // 10000 ms 任务1完成
// addTask(5000, 2) // 5000 ms 任务2完成
// addTask(3000, 3) // 8000 ms 任务3完成
// addTask(4000, 4) // 12000 ms 任务4完成
// addTask(5000, 5) // 15000 ms 任务5完成