Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js 函数实现 #10

Open
EchoZhaoH opened this issue May 1, 2020 · 0 comments
Open

js 函数实现 #10

EchoZhaoH opened this issue May 1, 2020 · 0 comments

Comments

@EchoZhaoH
Copy link
Owner

EchoZhaoH commented May 1, 2020

currying

function currying() {
    const arr = Array.from(arguments) 
    const fn = arr.shift()
    let rest = arr
    if (rest.length === fn.length) {
        return fn.apply(this, rest)
    }
    return function loop() {
        const args = rest.concat(...Array.from(arguments))
        if (args.length === fn.length) {
           return fn.apply(this, args)
        } else {
               return loop.apply(this, args)
        }
    }
}

new 的实现过程

function NewInstance(parent) {
    const obj = Object.create({})
    obj.__proto__ = Object.create(parent.prototype)
    const result = parent.apply(obj)
    return result instanceof Object ? result : obj 
}

instanceof 实现过程

function instanceofClone(child, parent) {
    let a = child.__proto__
    while(a) {
        if (a === parent.prototype) return true
         a = a.__proto__
    }
    return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant