当前位置:网站首页>js implements a bind function
js implements a bind function
2022-08-03 05:12:00 【captain on board】
欢迎来访船长在船上的博客,如有疑问可以留言、评论,看到后会及时回复.
目录
原理:通过 apply 或者 call 方法来实现
区别:function.apply(obj,argArray)//argArray为数组
function.call(obj,arg1,arg2,arg3…)//The following parameters are list items
apply()和call()The function is the same when no parameters are passed,But they accept different parameters,applyThe second parameter can only accept an array,并且apply()only two parameters,而call()Multiple argument list items are accepted since the second argument,There can be multiple parameters.
(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;
}
文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复.
边栏推荐
- [Harmony OS] [ARK UI] ETS context basic operations
- idea使用@Autowired注解爆红原因及解决方法
- MySQL 出现 The table is full 的解决方法
- 常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
- PotPlayer实现上班摸鱼电视自由
- 数据库基本概述与SQL概述
- 接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
- 刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
- 修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔
- Bubble sort in c language structure
猜你喜欢
typescript40-class类的保护修饰符
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
How to prepare for the test interface test data
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
Interface Test Framework Practice (4) | Get Schema Assertion
移动流量的爆发式增长,社交电商如何选择商业模式
【Harmony OS】【FAQ】鸿蒙问题合集1
IO进程线程->线程->day5
修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔
MCM box model modeling method and source analysis of atmospheric O3
随机推荐
自组织是管理者和成员的双向奔赴
Detailed explanation of MOSN reverse channel
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
Where is the value of testers
MySql 创建索引
Interface testing framework of actual combat (2) | interface request assertion
The problem that the rosbag tool plotjuggler cannot open rosbag
Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures
typescript49-交叉类型
GIS数据漫谈(五)— 地理坐标系统
How to prepare for the test interface test data
【Biotin Azide|cas:908007-17-0】Price_Manufacturer
打破传统电商格局,新型社交电商到底有什么优点?
WinForm的控件二次开发
Unity2D horizontal board game tutorial 6 - enemy AI and attack animation
MOSN 反向通道详解
RequestContextHolder
【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
Redis连接不上的报错解决方案汇总