手寫call,apply方法實現

callapp

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;
}

 

applythis

Function.prototype.myApply = function(object,arr){
    object.__proto__._fn = this;
    var result = object._fn(...arr);
    delete object.__proto__._fn;
    return result;
}
相關文章
相關標籤/搜索