Javascript系列(六):手写call/apply/bind函数

本文详细介绍了JavaScript中call、apply和bind三个方法的共同点和不同点,它们都用于改变函数的this指向。call和apply的区别在于参数传递方式,call使用逗号隔开,apply则接受一个参数数组。bind则返回一个新的函数,保留原this并允许延迟调用。同时,文中提供了这三个方法的手写实现,帮助理解其工作原理。
摘要由CSDN通过智能技术生成

简单介绍

共同点:

call apply bind 三个函数是Function对象中自带的方法,都是用来改变函数的this对象指向。

不同点:

  1. 第一个参数都是this要指向的对象,也就是想指定的上下文;
  2. call的第二个参数以逗号隔开,apply传入的是数组,bind第二个参数以逗号隔开;
  3. bind 是返回对应函数,便于稍后调用;apply 、call 则是立即调用;

手写call函数

Function.prototype.myCall = function(thisArg, ...args) {
     声明一个独有的Symbol属性, 防止fn覆盖已有属性
    const fn = Symbol('fn')    
    // 若没有传入this, 默认绑定window对象    
    thisArg = thisArg || window
    // this指向调用call的对象,即我们要改变this指向的函数    
    thisArg[fn] = this              
    const result = thisArg[fn](...args)  // 执行当前函数
    delete thisArg[fn]              // 删除我们声明的fn属性
    return result                  // 返回函数执行结果
}

手写apply函数

Function.prototype.myApply = function(thisArg, args) {
     // 声明一个独有的Symbol属性, 防止fn覆盖已有属性
    const fn = Symbol('fn')   
    // 若没有传入this, 默认绑定window对象    
    thisArg = thisArg || window  
    // this指向调用call的对象,即我们要改变this指向的函数  
    thisArg[fn] = this              
    const result = thisArg[fn](...args)  // 执行当前函数
    delete thisArg[fn]              // 删除我们声明的fn属性
    return result                  // 返回函数执行结果
}

手写bind函数

ES6的简单写法

Function.prototype.myBind = function(thisArg, ...args){
    const fn = this;
    return function(...args1){
         return fn.apply(thisArg,...args,...args1);
     }
}

ES5+支持new

Function.prototype.myBind = function (thisArg, ...args) {
    if (typeof this !== 'function') {
        throw new TypeError('Error')
    }
    var self = this
    var fbound = function () {
    // 如果当前函数的this指向的是构造函数中的this 则判定为new 操作
        self.apply(this instanceof self ? this : thisArg, args.concat(Array.prototype.slice.call(arguments)))
    }
    // 继承原型上的属性和方法
    fbound.prototype = self.prototype;
    return fbound;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值