当前位置:网站首页>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;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复。
边栏推荐
- Talking about GIS Data (5) - Geographic Coordinate System
- MySQL 入门:Case 语句很好用
- 【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
- 移动流量的爆发式增长,社交电商如何选择商业模式
- 探索性测试的概念及方法
- CobalStrike(CS)基础超级详细版
- Shell conditional statement judgment
- Unity2D horizontal board game tutorial 6 - enemy AI and attack animation
- 【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
- VR全景展打造专属元宇宙观展空间
猜你喜欢
随机推荐
Bubble sort in c language structure
数字化时代,企业如何建立自身的云平台与商业模式的选择?
6.神经网络剖析
typescript44-对象之间的类兼容器
MySQL 入门:Case 语句很好用
用户密码验证
2022/08/02 学习笔记 (day22) 多线程
Kotlin-Flow common encapsulation class: the use of StateFlow
IO process thread -> thread -> day5
社交电商如何做粉丝运营?云平台怎么选择商业模式?
rosbag工具plotjuggler无法打开rosbag的问题
Get the Ip tool class
User password encryption tool
探索性测试的概念及方法
索引创建、删除与使用
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
12.机器学习基础:评估机器学习模型
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
【Harmony OS】【FAQ】鸿蒙问题合集1
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability









