手写js中call、apply、bind函数

server/2024/9/25 9:37:45/

手写call

原理

将需要改变this指向的函数暂时性的设置为需要设置this指向的对象的函数。

代码

javascript">// 定义mycall方法,所有函数对象都是Function对象
Function.prototype.mycall = function (thisArg, ...args) {// 设置this,此时this指向原函数,之后将指向thisArgthisArg.f = this// 接收剩余参数并得到结果const res = thisArg.f(...args)// 移除函数delete thisArg.f// 返回结果return res
}// 测试样例
const person = {name: '南辞w'
}
function func(numA, numB) {console.log(this)console.log(numA, numB)return numA + numB
}
const res = func.mycall(person, 2, 8)
console.log('返回值为', res)

调优

对象中可能会出现函数重名冲突,可以利用Symbol()进行处理

调优后代码

javascript">Function.prototype.mycall = function (thisArg, ...args) {const key = Symbol('key')thisArg[key] = thisconst res = thisArg[key](...args)delete thisArg[key]return res
}const person = {name: '南辞w'
}
function func(numA, numB) {console.log(this)console.log(numA, numB)return numA + numB
}
const res = func.mycall(person, 2, 8)
console.log('返回值为', res)

手写apply

原理

改变this的方法同上,但接收的参数为数组形式,可以之间用 '...' 来展开接收

代码

javascript">Function.prototype.myapply = function (thisArg, args) {const key = Symbol('key')thisArg[key] = thisconst res = thisArg[key](...args)delete thisArg[key]return res
}const person = {name: '南辞w'
}function func(numA, numB, numC) {console.log(this)console.log(numA, numB, numC)return numA + numB + numC
}const res = func.myapply(person, [2, 8, 10])
console.log('返回值为', res)

手写bind

原理

改变this指向同上,但是此函数返回的是新的函数,并且可以分批次传值,前后次序不要弄混了

代码

javascript">Function.prototype.mybind = function (thisArg, ...args) {return (...reArgs) => {return this.call(thisArg, ...args, ...reArgs)}
}const person = {name: '南辞w'
}
function func(numA, numB, numC, numD) {console.log(this)console.log(numA, numB, numC, numD)return numA + numB + numC + numD
}
const bindFunc = func.mybind(person, 1, 2)
const res = bindFunc(3, 4)
console.log('返回值为', res)


http://www.ppmy.cn/server/121757.html

相关文章

C++三大特性之多态

前言 关于多态,是c中最重要的东西,通过虚函数来实现多态这种特性 一、多态的概念 多态是面向对象编程(OOP)中的一个重要概念,它允许对象在不同的上下文中以不同的方式表现。这意味着相同的接口(函数、方法&…

[大语言模型-工程实践] 手把手教你-基于Ollama搭建本地个人智能AI助理

[大语言模型-工程实践] 手把手教你-基于Ollama搭建本地个人智能AI助理 Note: 草稿优化中,持续更新,相关代码将统一提供出来~ 1. Ollama简介 Ollama 是一个用于在本地环境中运行和定制大型语言模型的工具。它提供了一个简单而高效的接口,用于…

SpringMVC详细使用总结教程

一、SpringMVC 1.1 介绍 Spring MVC 是 Spring 框架中的一个模块,用于构建基于 Java 的 Web 应用程序。它基于 MVC(Model-View-Controller)架构模式,提供了一种灵活而强大的方式来开发 Web 应用程序。Spring MVC 框架充分利用了…

loadrunner个人笔记

创建场景配置: 两个同时 去四:日志、时间、模拟、其他自动事务 加一:首选项 1、写脚本,沟通官方、文件打印扫描 MFI-sw.support.gsd.imsc.sda.globalopentext.com support.casemicrofocus.com 支持资源 | Micro Focus | OpenT…

MyBatis-config.xml核心配置

MyBatis-config.xml 包含了会深深影响MyBatis行为的设置和属性信息,配置文档的顶层结构如下 environments(环境配置) environments用于配置数据库的URL信息,MyBatis-config可以动态配置多个数据源,用于连生产、预发、…

Linux学习 重定向 管道 流

重定向 管道 流 在 Linux 中一个命令的去向可以有 3 个地方:终端、文件、作为另外一个命令的入参。 而命令一般都是通过键盘输入,然后输出到终端、文件等地方,它的标准用语是 stdin 、 stdout 以及 stderr 。 标准输入stdin,终端接…

Ansible 自动化运维工具的使用

1 说明 1.1 Ansible简介 ansible是一种流行的自动化运维工具,基于python2-paramiko模块开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令功能。 ansible是基于模块工作的,本身没有批量部署的能力…

python-list-append-method

Python 列表追加()方法 原文:https://www.geeksforgeeks.org/python-list-append-method/ Python List 追加() 方法用于在列表的末尾追加和添加元素。 **语法:**列表.追加(项) 参数: **项:**列表末尾要添加的项 返回: 该方法不返回值 示例 1:向列表中添加项目 计算机编程语言 …