2025.2.24 小结

// class MyPromise {
//     static PENDING = 'pending'
//     static FULFILLED = 'fulfilled'
//     static REJECTED = 'rejected'

//     #status = MyPromise.PENDING
//     #result = undefined
//     #queue = []

//     constructor(excutor) {
//         const resolve = (data) => {
//             this.#changeState(MyPromise.FULFILLED, data)
//         }
//         const reject = (err) => {
//             this.#changeState(MyPromise.REJECTED, err)
//         }

//         try{
//             excutor(resolve, reject)
//         } catch(e) {
//             reject(e)
//         }
//     }

//     #isPromiseLike(value) {
//         return (typeof value === 'object' || typeof value === 'function') && typeof value.then === 'function'
//     }

//     #runMicroTask (fn) {
//         if(typeof queueMicrotask === 'function') {
//             queueMicrotask(fn)
//         } else if(typeof Promise === 'function') {
//             Promise.resolve().then(fn)
//         } else if (process && typeof process.nextTick === 'function') {
//             process.nextTick(fn)

//         } else if(typeof MutationObserver === 'function') {
//             const textNode = document.createTextNode('')
//             const ob = new MutationObserver(fn)
//             ob.observer(textNode, {
//                 CharacterData:true
//             })
//             textNode.textNode = '1'
//         } else {
//             setTimeout(fn, 0)
//         }
//     }

//     #changeState(t, d) {
//         if(this.#status !== MyPromise.PENDING) return 
//         this.#status = t
//         this.#result = d 
        
//         this.#run()
//     }

//     #repeat(cb, resolve, reject) {
//         const settled = this.#status === MyPromise.FULFILLED ? resolve : reject
        
//         if(typeof cb !== 'function') {
//             settled(this.#result)
//         } else {
//             try {
//                 const d = cb(this.#result)   
//                 if(this.#isPromiseLike(d)) {
//                     d.then(resolve, reject)
//                 } else {
//                     resolve(d)
//                 }                              
//             } catch(e) {
//                 reject(e)
//             }
//         }
//     }

//     #run() {
//         this.#runMicroTask(() => {
//             if(this.#status === MyPromise.PENDING) return 

//             while(this.#queue.length) {              
//                 const {
//                     onFulfilled,
//                     onRejected,
//                     resolve,
//                     reject
//                 } = this.#queue.shift()
    
//                 if(this.#status === MyPromise.FULFILLED) {
//                     this.#repeat(onFulfilled, resolve, reject)
//                 } else if(this.#status === MyPromise.REJECTED) {
//                     this.#repeat(onRejected, resolve, reject)
//                 }
                
//             }
//         }) 
//     }
    

//     then(onFulfilled, onRejected) {
//         const p = new MyPromise((resolve, reject) => {
//             this.#queue.push({
//                 onFulfilled,
//                 onRejected,
//                 resolve,
//                 reject
//             })

//             this.#run()
//         })
//         return p
//     }
    
// }
// const p = new MyPromise((resolve, reject) => {
//     // resolve(123)  or
//     setTimeout(_ => {
//         resolve(123)
//     },2000)
// })

// p.then(res => {
//     console.log('1', res);
// }, err => {
//     console.log('1', err);
// }).then(res => {
//     console.log('2', res);
// }, err => {
//     console.log('2', err);
// })


// p.then(res => {
//     console.log('3', res);
// }, err => {
//     console.log('3', err);
// })

// Promise.prototype.MyAll = function(promises) {

//     let res, rej

//     const p = new Promise(((resolve, reject) => {
//         res = resolve
//         rej = reject
//     }))

//     const result = []
//     let idx = 0

//     for(let pro of promises) {
//         const i = idx 
//         idx ++
//         Promise.resolve(pro).then(res => {
//             result[i] = res
//             idx --
//             if(idx === 0) {
//                 res(result)
//             }
//         }, rej)
//     }

//     if(promises.length === 0)  {
//         res([])
//     }

//     return p
// }


// Function.prototype.myCall = function ( p, ...args ) {
//     const obj = typeof p === 'object' && p !== null ? p : Object(p)
//     const fn = this
//     const k = Symbol()
//     obj[k] = fn
//     const d = obj[k](...args)
//     delete obj[k]
//     return d
// }

// Function.prototype.myApply = function ( p, args ) {
//     const obj = typeof p === 'object' && p !== null ? p : Object(p)
//     const fn = this
//     const k = Symbol()
//     obj[k] = fn
//     const d = obj[k](args)
//     delete obj[k]
//     return d
// }

// Function.prototype.myBind = function ( p, args1 ) {
//     const obj = typeof p === 'object' && p !== null ? p : Object(p)
//     const fn = this
//     return function (...args2) {
//         const k = Symbol()
//         obj[k] = fn
//         const args = [...args1, ...args2]
//         const d = obj[k](...args)
//         delete obj[k]
//         return d
//     }
// }

// function foo (a,b) {
//     console.log(a,b);
// }

// const obj = {
//     a:1,
//     b:2
// }

// foo.myCall(1, 2, 3)


// function deepClone ( value ) {
//     const cache = new Map()
    
//     function _deepClone(o) {
//         if(o === null || typeof o !== 'object') {
//             return o
//         }

//         if(cache.has(o)) {
//             return cache.get(o)
//         }
//         const result = Array.isArray(o) ? [] : {}
//         cache.set(o, result)

//         for(let k in o) {
//             result[k] = _deepClone(o[k])
//         }

//         return result
//     }

//     return _deepClone(value)
// }

function deepClone (value) {
    const cache = new Map()
    function _deepClone(val) {

        if(typeof val !== 'object' || val === null) {
            return val
        }

        // 解决循环引用
        if(cache.has(val)) {
            return cache.get(val)
        }

        const result = Array.isArray(val) ? [] : {}

        cache.set(val, result)

        for(let k in val) {
            result[k] = _deepClone(val[k])
        }

        return result
    }

    const temp = _deepClone(value)
    console.log(cache)
    return temp
}

const obj = {
    a:{
        c:1
    },
    b:[]
}

obj.b.push(obj)
const ooo = deepClone(obj)
console.log(ooo);
console.log(ooo === obj);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值