当前位置:网站首页>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;
}
文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复.
边栏推荐
猜你喜欢
随机推荐
unity2D横板游戏教程6-敌人AI以及受击动画
js中的闭包
typescript44-对象之间的类兼容器
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
Create a tree structure
Redis缓存雪崩、缓存穿透、缓存击穿
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
The problem that the rosbag tool plotjuggler cannot open rosbag
CobalStrike(CS)基础超级详细版
高可用 两地三中心
Interface testing framework combat (3) | JSON request and response assertion
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
Secondary development of WinForm controls
shell script loop statement
安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)
接口和协议
JS bottom handwriting
MySQL 入门:Case 语句很好用
常见荧光染料修饰多种基团及其激发和发射波长数据一览数据