理解并手写 bind() 函数
有了对call()、apply()的前提分析,相信bind()我们也可以手到擒来。
参考前两篇:'对call()函数的分析' 和 '对apply()函数的分析',我们可以先得到以下代码:
Function.prototype.myBind = function(obj){
// 判断调用对象是否为函数
if(typeof this !== 'function'){
console.error('type error!')
}
// 判断绑定的对象
obj = obj || window;
}
bind()函数与call()函数参数的格式相同,不同的是bind()返回的是一个函数。
Function.prototype.myBind = function(obj){
if(typeof this !== 'function'){
console.error('type error!')
}
obj = obj || window;
// 获取正确参数
let args = [...arguments].slice(1);
// 返回函数
return function Fn(){};
}
使用bind()函数来对this进行绑定,可以之间在内部使用 apply() 。
Function.prototype.myBind = function(obj){
if(typeof this !== 'function'){
console.error('type error!')
}
obj = obj || window;
let args = [...arguments].slice(1);
return function Fn(){
// 直接使用apply()
return this.apply();
};
}
这个时候会发现,我们绑定的this是window,而不是我们想要的’this‘环境,可以使用that作为中间量。
Function.prototype.myBind = function(obj){
if(typeof this !== 'function'){
console.error('type error!')
}
obj = obj || window;
// 在这里借助that
that = this;
let args = [...arguments].slice(1);
return function Fn(){
return that.apply();
};
}
然后我们需要考虑apply()中传递的参数,这里我们使用数组的concat()方法,得到apply中传递的数组。
Function.prototype.myBind = function(obj){
if(typeof this !== 'function'){
console.error('type error!')
}
obj = obj || window;
that = this;
let args = [...arguments].slice(1);
return function Fn(){
// 传递两个参数(绑定对象,数组)
return that.apply(obj,args.concat(...arguments));
};
}
最后需要判断函数作为构造函数的情况,这个时候需要传入当前函数的 this 给 apply 调用,其余情况都传入指定的上下文对象。
Function.prototype.myBind = function(obj){
if(typeof this !== 'function'){
console.error('type error!')
}
obj = obj || window;
that = this;
let args = [...arguments].slice(1);
return function Fn(){
// 根据调用方式,传入不同绑定值
return that.apply(
this instanceof Fn ? this : obj,
args.concat(...arguments));
};
}
最后通过一个例子,来验证是否达到bind()的功能要求。
Function.prototype.myBind = function(obj){
if(typeof this !== 'function'){
console.error('type error!')
}
obj = obj || window;
that = this;
let args = [...arguments].slice(1);
return function Fn(){
return that.apply(
this instanceof Fn ? this : obj,
args.concat(...arguments)
);
}
}
let dog = {
name: '狗',
eat(food1, food2) {
console.log(this.name + '爱吃' + food1 + food2);
}
}
let cat = {
name: '猫',
}
dog.eat.bind(cat, '鱼', '肉')(); // 猫爱吃鱼肉
dog.eat.mybind(cat, '鱼', '肉')(); // 猫爱吃鱼肉
理解并手写 bind() 函数的更多相关文章
- 理解并手写 call() 函数
手写自己的call,我们要先通过call的使用,了解都需要完成些什么功能? call()进行了调用,是个方法,已知是建立在原型上的,使用了多个参数(绑定的对象+传递的参数). 我们把手写的函数起名为m ...
- 手写bind函数
实现bind函数 参考MDN提供的Polyfill方案 Function.prototype.myBind = function(context){ //这里对调用者做一个判断,如果不是函数类型,直接 ...
- 理解并手写 apply() 函数
apply()函数,在功能上类似于call(),只是传递参数的格式有所不同. dog.eat.call(cat, '鱼', '肉'); dog.eat.apply(cat, ['鱼', '肉']); ...
- 前端面试题整理——手写bind函数
var arr = [1,2,3,4,5] console.log(arr.slice(1,4)) console.log(arr) Function.prototype.bind1 = functi ...
- WPF启动流程-自己手写Main函数
WPF一般默认提供一个MainWindow窗体,并在App.Xaml中使用StartupUri标记启动该窗体.以下通过手写实现WPF的启动. 首先先介绍一下VS默认提供的App.Xaml的结构,如下图 ...
- 优雅手撕bind函数(面试官常问)
优雅手撕bind函数 前言: 为什么面试官总爱让实现一个bind函数? 他想从bind中知道些什么? 一个小小的bind里面内有玄机? 今天来刨析一下实现一个bind要懂多少相关知识点,也方便我们将零 ...
- python 精华梳理(已理解并手写)--全是干货--已结
基础部分 map,reduce,filter,sort,推导式,匿名函数lambda , 协程,异步io,上下文管理 自定义字符串转数字方法一不使用reduce import re def str2i ...
- C++之手写strlen函数
代码: int strlen(const char *str){ assert(str!=NULL); intlen=; while((*str++)!='\0') len++; return len ...
- js面试题之手写节流函数和防抖函数
函数节流:不断触发一个函数后,执行第一次,只有大于设定的执行周期后才会执行第二次 /* 节流函数:fn:要被节流的函数,delay:规定的时间 */ function throttle(fn,dela ...
随机推荐
- jQuery EasyUI 的editor组件使用
问题:最近在优化一个项目时,前端用到了 easyui这个插件来实现表格,搞了很久,才实现出一部分功能,但是还是有很多地方不熟悉,故记录一下,以后再研究 第一个实例------------------- ...
- 实现表单input文本框不可编辑的三种方法
感谢原文作者:青灯夜游 原文链接:https://www.php.cn/div-tutorial-413133.html 目录 问题 实现方式 1.οnfοcus=this.blur() 2.read ...
- JAVA类加载器一 父类委托机制
感谢原文作者:不将就! 原文链接:https://www.cnblogs.com/byron0918/p/5770653.html 类加载器负责将.class文件加载到内存中,并为之生成对应的Clas ...
- Shell之sed编辑器
Shell之sed编辑器 目录 Shell之sed编辑器 一.sed编辑器 1. sed编辑器概述 2. sed编辑器的工作流程 二.sed命令 1. 命令格式 2. 常用选项 3. 常用操作 三.操 ...
- 一次Kafka内存泄露排查经过
一.现象 服务部署后内存总体呈上升趋势 二.排查过程 通过go tool pprof收集了三天内存数据 2月11号数据: 2月14号数据: 2月15号数据: 可以看到newPartitionProdu ...
- 系统操作命令实践 下(系统指令+增删改查+vim编辑器)
目录 1.考试 2.今日问题 3.今日内容 4.复制文件 4.移动文件 Linux文件查看补充 cat , nl 5.删除文件 6.系统别名 7.vi/vim编辑器 系统操作命令实践 下(系统指令+增 ...
- nginx反向代理初体验
需求:部署两台tomcat,默认监听端口分别是8080和8081.访问nginx服务时,自动跳转到相应tomcat服务. 先部署一台机器:就宿主机上tomcat服务: 修改nginx配置:vim ng ...
- Redis入门与实践(附项目真实案例代码)
我是3y,一年CRUD经验用十年的markdown程序员常年被誉为优质八股文选手 今天继续更新austin项目,如果还没看过该系列的同学可以点开我的历史文章回顾下,在看的过程中不要忘记了点赞哟!建议 ...
- java集合专题 (ArrayList、HashSet等集合底层结构及扩容机制、HashMap源码)
一.数组与集合比较 数组: 1)长度开始时必须指定,而且一旦指定,不能更改 2)保存的必须为同一类型的元素 3)使用数组进行增加/删除元素-比较麻烦 集合: 1)可以动态保存任意多个对象,使用比较方便 ...
- 【C# 线程】线程局部存储(TLS)理论部分 ThreadStatic|LocalDataStoreSlot|ThreadLocal<T>
线程本地存储(TLS:Thread Local Storage) 线程本地存储(Thread Local Storage),字面意思就是专属某个线程的存储空间.变量大体上分为全局变量和局部变量,一个进 ...