手写call,apply方法实现

call

Function.prototype.myCall = function(){
    var object = arguments[0];
    var arr = [];
    for(var i = 1; i < arguments.length; i++){
        arr.push(arguments[i]);
    }
    object.__proto__._fn = this;
    var result = object._fn(...arr);
    delete object.__proto__._fn;
    return result;
}

 

apply

Function.prototype.myApply = function(object,arr){
    object.__proto__._fn = this;
    var result = object._fn(...arr);
    delete object.__proto__._fn;
    return result;
}

 

posted @ 2019-05-21 09:07  卖鱼熊  阅读(1374)  评论(2编辑  收藏  举报