设为首页 加入收藏

TOP

手写call,apply方法实现
2019-09-17 18:47:30 】 浏览:20
Tags:手写 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;
}

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇js 调用栈机制与ES6尾调用优化介绍 下一篇js设置页面全屏

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目