当前位置:网站首页>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;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复.
边栏推荐
- RequestContextHolder
- 【Biotin Azide|cas:908007-17-0】Price_Manufacturer
- typescript40-class类的保护修饰符
- 接口测试如何准备测试数据
- Flink状态
- Interface Test Framework Practice (4) | Get Schema Assertion
- User password verification
- [Developers must see] [push kit] Collection of typical problems of push service service 2
- 2022/08/02 Study Notes (day22) Multithreading
- rosbag工具plotjuggler无法打开rosbag的问题
猜你喜欢

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

MCM box model modeling method and source analysis of atmospheric O3

【Harmony OS】【ArkUI】ets开发 图形与动画绘制

Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures

RequestContextHolder

DFS's complement to pruning

【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接

typescript39-class类的可见修饰符

Super handy drawing tool is recommended

CAD有生僻字如何打出来、如何提交软件相关问题或建议?
随机推荐
表的创建、修改与删除
closures in js
接口测试框架实战(二)| 接口请求断言
2022/08/02 Study Notes (day22) Multithreading
c语言结构体中的冒泡排序
Redis缓存雪崩、缓存穿透、缓存击穿
[Harmony OS] [ArkUI] ets development graphics and animation drawing
mysql 创建索引的三种方式
JS底层手写
Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
Interface testing framework of actual combat (2) | interface request assertion
【开发者必看】【push kit】推送服务服务典型问题合集2
tag单调栈-单调栈预备知识-lt.739. 每日温度
typescript46-函数之间的类型兼容性
presto安装部署教程
2022暑假牛客多校联赛第一场
Kotlin-Flow常用封装类:StateFlow的使用
在树莓派上搭建属于自己的网页(2)
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
Kotlin-Flow common encapsulation class: the use of StateFlow