重写内置call、bind

 重写内置call
/*
 * 重写内置的CALL:把需要执行的函数和需要改变的THIS关联在一起  
 *   + context.xxx=this  obj.fn=fn;
 *   + context.xxx()  obj.fn();
 */
Function.prototype.call = function call(context, ...params) {
    // this->fn  context->obj  params->[10,20]
    context == null ? context = window : null;
    // 需要保证context必须是对象类型的值:只有对象才能设置属性
    !/^(object|function)$/.test(typeof context) ? context = Object(context) : null;
    let self = this,
        result = null,
        key = Symbol('KEY'); //新增的属性名保证唯一性,防止污染了原始对象中的成员
    context[key] = self;
    result = context[key](...params);
    delete context[key]; //用完后移除自己新增的属性
    return result;
};
 
function fn(x, y) {
    console.log(this, x, y);
}
let obj = {
    name: 'zhufeng',
    fn: 100
};
 
fn.call(obj, 10, 20); 
重写内置bind

柯理化思想「预处理思想」 

Function.prototype.bind = function bind(context, ...outerArgs) {
  // this->fn context->obj outerArgs->[10,20]
  let self = this;
  return function (...innerArgs) {
    // innerArgs->[ev]
    elf.call(context, ...outerArgs.concat(innerArgs));
  };
};

function fn(x, y, ev) {
  console.log(this, x, y, ev);
}
let obj1 = {
  name: 'zhufeng'
};

/* document.body.onclick = function (ev) {
    fn.call(obj, 10, 20, ev);
}; */
document.body.onclick = fn.bind(obj1, 10, 20);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值