欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

手写call,apply方法实现

程序员文章站 2022-08-17 16:23:19
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;
}