js简单实现call,apply和bind函数

本文详细介绍了如何使用JavaScript模拟实现Function对象的call、apply和bind方法,通过实例展示了这三个方法在对象上下文切换和参数传递中的应用,加深了对JavaScript函数调用的理解。
摘要由CSDN通过智能技术生成

1.实现call2函数

const obj = {
    name: '小花',
   };
const obj1 = {
    name: '小草',
};
function test(a, b) {
    console.log(this.name);
    console.log(a + b );
}
//模拟call实现
Function.prototype.call2=function(context){
    if(context == null){
        var context = Window;
    }else{
        var context = Object(context);
    }
    if(typeof this != 'function'){
        throw TypeError(this + 'is not a function');
    }
    var fn = Symbol();
    context[fn] = this;

    //取传入的参数
    let args = [];
    for (let index = 1; index < arguments.length; index++) {
        args.push('arguments['+ index + ']')  
    }
    //执行函数
    var result = eval('context[fn]('+args+')');
    return result;
}
test.call(obj1,1,10)
test.call2(obj1,1,5);

2.实现apply2函数 

const obj = {
    name: '小花',
   };
const obj1 = {
    name: '小草',
};
function test(a, b) {
    console.log(this.name);
    console.log(a + b );
}

//模拟apply实现
Function.prototype.apply2=function(context,arr){
    if(context == null){
        var context = Window;
    }else{
        var context = Object(context);
    }
    if(typeof this != 'function'){
        throw TypeError(this + 'is not a function');
    }
    var fn = Symbol();
    context[fn] = this;

    var result
    //执行函数
    if(arr){
        result = eval(context[fn](...arr))
    }else{
        context[fn]()
    }
    return result;
}
test.apply2(obj,[1,50]);

 3.在call和apply的基础上实现applyFn函数

const obj = {
    name: '小花',
   };
const obj1 = {
    name: '小草',
};
function test(a, b) {
    console.log(this.name);
    console.log(a + b );
}
//模拟bind实现
Function.prototype.bindFn = function(context){
    if(typeof this != 'function'){
        throw TypeError(context + 'is not a function');
    }
    //获取第一次的对象和传过来的参数
    var self = this;
    //去掉arguments类数组中第一个元素,也就是this的值,得到第一次传入的参数args1
    var args1 = [].slice.call(arguments,1)

    //返回一个已经绑定好this值的bound函数,并且加入第二次传入的参数
    var bound = function(){
        var args2 = [].slice.call(arguments)
        return self.apply(context,args1.concat(args2))
    }
    return bound;
}
var bound = test.bindFn(obj,1);
bound(20);

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值