当前位置:网站首页>js实现一个 bind 函数
js实现一个 bind 函数
2022-08-03 04:55:00 【船长在船上】
欢迎来访船长在船上的博客,如有疑问可以留言、评论,看到后会及时回复。

目录
原理:通过 apply 或者 call 方法来实现
区别:function.apply(obj,argArray)//argArray为数组
function.call(obj,arg1,arg2,arg3…)//后面的参数为列表项
apply()和call()不传参数时候功能是相同的,但是它们接受的参数不同,apply的第二个参数只能接受一个数组,并且apply()只能是二个参数,而call()的第二个参数起接受的多个参数列表项,参数是可以多个。
(1)初始版本
Function.prototype.bind=function(obj,arg){
var arg=Array.prototype.slice.call(arguments,1);
var context=this;
return function(newArg){
arg=arg.concat(Array.prototype.slice.call(newArg));
return context.apply(obj,arg);
}
}(2) 原型链
Function.prototype.bind=function(obj,arg){
var arg=Array.prototype.slice.call(arguments,1);
var context=this; var bound=function(newArg){
arg=arg.concat(Array.prototype.slice.call(newArg));
return context.apply(obj,arg);
}
var F=function(){}
//这里需要一个寄生组合继承
F.prototype=context.prototype;
bound.prototype=new F();
return bound;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复。
边栏推荐
猜你喜欢

How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use

StarRocks July Community Update

typescript41-class类的私有修饰符

测试人员的价值体现在哪里

DFS对剪枝的补充

【Harmony OS】【ARK UI】ETS 上下文基本操作
![[Harmony OS] [ARK UI] ETS context basic operations](/img/40/d5924477c42e2b3246eb212f4be534.png)
[Harmony OS] [ARK UI] ETS context basic operations

荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide

【 Harmony OS 】 【 ano UI 】 lightweight data storage

Super handy drawing tool is recommended
随机推荐
JS bottom handwriting
【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
接口测试如何准备测试数据
CobalStrike(CS)基础超级详细版
在线密码生成工具推荐
在竞争白热化的电商行业,链动2+1为什么还有企业在用
Practical application of WebSocket
接口测试实战| GET/POST 请求区别详解
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
2022 Henan Mengxin League Game (4): Zhengzhou University of Light Industry E - Sleep Well
DFS's complement to pruning
4.深度学习的几何解释与梯度的优化
shell script loop statement
Interface Test Framework Practice (4) | Get Schema Assertion
Kotlin-Flow common encapsulation class: the use of StateFlow
超好用的画图工具推荐
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
6.神经网络剖析
[Developers must see] [push kit] Collection of typical problems of push service service 2
BIOTIN ALKYNE CAS:773888-45-2价格,供应商