2025.2.25 小结

/**
 * 混合防抖函数
 */
// 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完成


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值