JSRUN 用代码说话
null
trackoverflow
JSRUN的第25687位用户
加入于 2019-06-01
上次活跃 2019-12-25



// function demo() {
//     return new Promise((resolve, reject) => {
//         setTimeout(function () {
//             const r = Math.random()
//             r >= 0.5 ? resolve(r) : reject(r)
//         }, 1000)
//     })
// }

// function handleDemo() {
//     demo().then((r1, r2) => {
//         console.log('success', r1, r2)
//     }).catch(r => {
//         console.log('error', r)
//     })
// }

// handleDemo()
// return

class RePromise {
    constructor(handle) {

        // 判断传入的是否是一个方法
        if (typeof handle !== 'function') {
            throw Error('Promise param is not a function .')
        }

        this.status = 'PENDING'
        this.value = undefined

        // 事件队列
        this.fulfilledQueues = []
        this.rejectedQueues = []

        // 捕获到传入的执行函数错误抛出
        try {
            handle(this.resolve.bind(this), this.reject.bind(this))
        } catch (e) {
            throw Error(e)
            this.reject(e)
        }
    }

    resolve(val) {
        if (this.status !== 'pending') return
        this.status = 'FULFILLED'
        this.value = val
    }

    reject(err) {
        if (this.status !== 'pending') return
        this.status = 'REJECTED'
        this.value = err
    }

    then(onFulfilled, onRejected) {
        const { status, value, fulfillQueues, rejectedQueues } = this

        // 如果当前状态为 等待
        if (status === 'PENDING') {
            fulfillQueues.push(onFulfilled)
            rejectedQueues.push(onRejected)
        }

        // 如果当前状态为 完成
        if (status === 'FULFILLED') {
            onFulfilled(value)
        }

        // 如果当前状态为 拒绝
        if (status === 'REJECTED') {
            onRejected(value)
        }

        return new RePromise((onFulfilledNext, onRejectedNext) => {})
    }
}

function test() {
    return new RePromise((resolve, reject) => {
        setTimeout(function () {
            const r = Math.random()
            r >= 0.5 ? resolve(r) : reject(r)
        }, 1000)
    })
}

test()
trackoverflow (null )- 2019-12-25 0 人
手动实现一个 Promise
class A {
    constructor() {
        this.nameA = 'a'
    }

    validateA() {
        consoel.log(this.nameA)
    }
}

class B extends A {
    constructor() {
        super()
        this.nameB = 'b'
    }

    validateB() {
        consoel.log(this.nameB)
    }
}


class C extends B {
    constructor() {
        super()
        this.nameC = 'c'
    }

    validateC() {
        consoel.log(this.nameC)
    }
}

console.log(C.prototype)
trackoverflow (null )- 2019-12-24 0 人
JavaScript 类
function Person(name) {
    this.name = name
}

Person.prototype.sayName = function() {
    console.log(this.name)
}

var p = Object.setPrototypeOf({}, Person.prototype)

Person.call(p, '123')

console.log(p)
trackoverflow (null )- 2019-12-24 0 人
手动实现 new 操作符
class A {
    constructor() {
        this.nameA = 'a'
    }

    validateA() {
        consoel.log(this.nameA)
    }
}

class B extends A {
    constructor() {
        super()
        this.nameB = 'b'
    }

    validateB() {
        consoel.log(this.nameB)
    }
}


class C extends B {
    constructor() {
        super()
        this.nameC = 'c'
    }

    validateC() {
        consoel.log(this.nameC)
    }
}

console.log(C.prototype)
trackoverflow (null )- 2019-12-24 0 人
JavaScript 类
Function.prototype._call = function (content = window) {
    content.fn = this
    let args = Array.prototype.slice.call(arguments, 1)
    content.fn(...args)
    delete content.fn
}


const foo = {
    value: 11
}

const bar = function (name, age) {
    console.log('name', name)
    console.log('age', age)
    console.log(this.value)
}

bar._call(foo, 1, 2, 3)
trackoverflow (null )- 2019-06-14 0 人
Javascript 手动实现 Function....
Array.prototype._reduce = function (fn, init) {
    const self = this
    for (let i = 0; i < this.length; i++) {
        if (init !== 0 && !init) {
            var res;
            if (!i) {
                res = fn.call(self, self[0], self[i + 1], i + 1, self)
            } else {
                if (i + 1 < self.length) {
                    res = fn.call(self, res, self[i + 1], i + 1, self)
                } else {
                    return res
                }
            }
        } else {
            if (!i) {
                res = fn.call(self, init, self[i], i, self)
            } else {
                res = fn.call(self, res, self[i], i, self)
                if (i + 1 === self.length) {
                    return res
                }
            }
        }
    }
}

// ; {
//     const arr = [12, 13, 14, 15]
//     console.log(
//         arr._reduce(function (pre, cur, index, arr) {
//             console.log(pre, cur, index, arr)
//             return pre + cur
//         })
//     )
// }
trackoverflow (null )- 2019-06-02 0 人
es5 for 循环实现一个 reduce
没有了
1/2 下一页