手写 call、apply、bind

一、手写 call

Function.prototype.Call = function(context, ...args) {
    // 当 context 为 undefined 或 null 时,context 指向window
    if (!context || context === null) {
        context = window
    }
    // 使用 Symbol 创建唯一的 key 值,防止与原有属性冲突
    let fn = Symbol();
    // 将当前函数挂载到 context 对象下,this 指向调用 call 的函数
    context[fn] = this
    // 隐式绑定 this,如执行 obj.foo(), foo 内的 this 指向 obj
    const res = context[fn](...args);
    // 执行完毕后,删除新增的属性
    delete context[fn];
    return res
}

二、手写 apply

// apply与call相似,只有第二个参数是一个数组,
Function.prototype.Apply = function(context, args) {
    if (!context || context === null) {
        context = window
    }
    let fn = Symbol()
    context[fn] = this
    const res = context[fn](...args)
    delete context[fn];
    return res
}

三、手写bind

Function.prototype.Bind = function(context, ...args) {
    if (!context || context === null) {
        context = window
    }
    
    let fn = this
    let f = Symbol()
    
    const result = function(...args1) {
        if (this instanceof fn) {
            // result 如果作为构造函数被调用, ths 指向的是 new 出来的对象
            // this instanceof fn,判断 new 出来的对象是否为 fn 的实例
            this[f] = fn
            let res = this[f](...args, ...args1)
            delete this[f]
            return res
        } else {
            // bind 返回的函数作为普通函数被调用时
            context[f] = fn
            let res = context[f](...args, ...args1)
            delete context[f]
            return res
        }
    }
    // 如果绑定的是构造函数 那么需要继承构造函数原型属性和方法
    // 实现继承的方式: 使用 Object.create
    result.prototype = Object.create(fn.prototype);
    return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值