手写简单的call bind apply

call

js 复制代码
  const Person1 = {
    fullName: function (country, city) {
      return this.firstName + this.lastName + " " + country + " " + city
    }
  }
  const Person2 = {
    firstName: "张",
    lastName: "三",
  }
  Person1.fullName.call(Person2, '中国', '河南') // 张三 中国 河南 

call的作用是改变了那个被执行的方法(也就是调用call的那个方法)的直接调用者!

而这个被执行的方法内部的this也会重新指向那个新的调用者,就是call方法所接收的第一个obj参数。

还有两个特殊情况就是当这个obj参数为null或者undefined的时候,this会指向window。

js 复制代码
Function.prototype.myCall = function (obj) {
    // 保存this 谁调用的myCall,this就指向谁,指向obj1.fun
    obj.fn = this
    // 保存参数
    let args = Array.from(arguments).slice(1)
    // 调用函数
    let result = obj.fn(...args)
    // 删除fn属性
    delete obj.fn
    return result
}
var obj1 = {
    num: 20,
    fun: function (n) {
        return this.num + n
    }
};
var obj2 = {
    num: 15,
};
let res = obj1.fun.myCall(obj2, 10)
console.log(res);

apply

js 复制代码
  const person = {
    fullName: function (country, city) {
      return this.firstName + this.lastName + " " + country + " " + city
    }
  }
  const newPerson = {
    firstName: "壮",
    lastName: "志国",
  }
  person.fullName.apply(newPerson, ['中国', '河南']) // 壮志国 中国 河南
js 复制代码
Function.prototype.myApply = function (obj) {
    let result
    // 保存this 谁调用的myCall,this就指向谁,指向obj1.fun
    obj.fn = this
    // 是否传参
    if (arguments[1]) {
        result = obj.fn(...arguments[1])
    } else {
        result = obj.fn()
    }
    delete obj.fn
    return result
}
var obj1 = {
    num: 20,
    fn: function (n, m) {
        return this.num + n + m
    }
};
var obj2 = {
    num: 15,
};
let res = obj1.fn.myApply(obj2, [10, 20])
console.log(res);

bind

js 复制代码
  const person = {
    fullName: function (country, city) {
      return this.firstName + this.lastName + " " + country + " " + city
    }
  }
  const newPerson = {
    firstName: "壮",
    lastName: "志国",
  }
   // 打印出fullName函数
  person.fullName.bind(newPerson, '中国', '河南')() // 壮志国 中国 河南
js 复制代码
Function.prototype.myBind = function (obj) {
    // 这里的 this/self 指的是需要进行绑定的函数本身,比如用例中的 man
    const self = this;
    // 获取 myBind 函数从第二个参数到最后一个参数(第一个参数是 obj)
    // 这里产生了闭包
    const args = Array.from(arguments).slice(1)
    return function () {
        // 这个时候的 arguments 是指 myBind 返回的函数传入的参数
        const bindArgs = [...arguments]
        // 合并
        return self.apply(obj, [...args, ...bindArgs]);
    };
};

const person = {
    name: 'zyj'
}

function man(age) {
    console.log(this.name);
    console.log(age)
}

const test = man.myBind(person)
test(18)//zyj 18
相关推荐
于慨19 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
石小石Orz19 小时前
油猴脚本实现生产环境加载本地qiankun子应用
前端·架构
从前慢丶19 小时前
前端交互规范(Web 端)
前端
CHU72903520 小时前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序
GISer_Jing20 小时前
Page-agent MCP结构
前端·人工智能
王霸天20 小时前
💥别再抄网上的Scale缩放代码了!50行源码教你写一个永不翻车的大屏适配
前端·vue.js·数据可视化
小领航20 小时前
用 Three.js + Vue 3 打造炫酷的 3D 行政地图可视化组件
前端·github
@大迁世界20 小时前
2026年React大洗牌:React Hooks 将迎来重大升级
前端·javascript·react.js·前端框架·ecmascript
PieroPc20 小时前
一个功能强大的 Web 端标签设计和打印工具,支持服务器端直接打印到局域网打印机。Fastapi + html
前端·html·fastapi
悟空瞎说20 小时前
深入 Vue3 响应式:为什么有的要加.value,有的不用?从设计到源码彻底讲透
前端·vue.js