JavaScript基础之--- 手写 apply方法 的实现

 

手写实现如下:

 

Function.prototype.myApply = fucntion(context) {
    if(typeof this !== 'function') {
        throw new TypeError('not a function!')
    }
    context = context || window
    context.fn = this
    let result
    if(arguments[1]) {
        result = context.fn(...arguments[1])
    }else{
        result = context.fn()
    }
    delete context.fn
    return result
}

 

posted @ 2020-03-25 09:58  见证LBJ  阅读(1282)  评论(0编辑  收藏  举报