当前位置:网站首页>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;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复.
边栏推荐
猜你喜欢
随机推荐
在线密码生成工具推荐
Windows 安装PostgreSQL
shell script loop statement
测试人员的价值体现在哪里
建立树形结构
shell脚本循环语句
接口测试实战| GET/POST 请求区别详解
「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
表的创建、修改与删除
Apache DolphinScheduler版本2.0.5分布式集群的安装
在树莓派上搭建属于自己的网页(1)
CAD有生僻字如何打出来、如何提交软件相关问题或建议?
Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
【Harmony OS】【FAQ】鸿蒙问题合集1
Get the Ip tool class
接口测试如何准备测试数据
MySql 创建索引
Shell之条件语句
DDL操作数据库、表、列
odps的临时查询能在写sql的时候就给结果一个命名不?









