当前位置:网站首页>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;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复。
边栏推荐
- closures in js
- 【Harmony OS】【ArkUI】ets开发 图形与动画绘制
- Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
- 软件开发的最大的区别是什么?
- 【Harmony OS】【ARK UI】Date 基本操作
- Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
- 私域流量引流方法?分享购火爆的商业模式,你值得拥有
- 接口测试实战| GET/POST 请求区别详解
- 接口测试框架实战(四)| 搞定 Schema 断言
- 在线密码生成工具推荐
猜你喜欢

【Harmony OS】【FAQ】鸿蒙问题合集1

How to prepare for the test interface test data

私域流量引流方法?分享购火爆的商业模式,你值得拥有

Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne

typescript42-readonly修饰符

Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface

DDL操作数据库、表、列

Coordinate knowledge in digital twin campus scenarios

shell script loop statement

Shell conditional statement judgment
随机推荐
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
Interface testing framework of actual combat (2) | interface request assertion
CAD有生僻字如何打出来、如何提交软件相关问题或建议?
unity2D横板游戏教程6-敌人AI以及受击动画
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
【Biotin Azide|cas:908007-17-0】Price_Manufacturer
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
2022/08/02 Study Notes (day22) Multithreading
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
MySQL 删除表数据,重置自增 id 为 0 的两个方式
rosbag工具plotjuggler无法打开rosbag的问题
IO process thread -> thread -> day5
CobalStrike(CS)基础超级详细版
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
typescript47-函数之间的类型兼容性
Interface testing framework combat (3) | JSON request and response assertion