当前位置:网站首页>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;
}
文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复.
边栏推荐
- 接口测试框架实战(四)| 搞定 Schema 断言
- 【软件工程之美 - 专栏笔记】35 | 版本发布:软件上线只是新的开始
- RequestContextHolder
- redis键值出现 xacxedx00x05tx00&的解决方法
- typescript44-对象之间的类兼容器
- 移动流量的爆发式增长,社交电商如何选择商业模式
- 【Harmony OS】【FAQ】鸿蒙问题合集1
- 在树莓派上搭建属于自己的网页(1)
- 安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)
- Interface test Mock combat (2) | Combined with jq to complete batch manual Mock
猜你喜欢
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
MCM箱模型建模方法及大气O3来源解析
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
c语言结构体中的冒泡排序
Install PostgreSQL on Windows
在树莓派上搭建属于自己的网页(2)
How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
【 Harmony OS 】 【 ano UI 】 lightweight data storage
redis键值出现 xacxedx00x05tx00&的解决方法
Where is the value of testers
随机推荐
打破传统电商格局,新型社交电商到底有什么优点?
接口测试如何准备测试数据
How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
在树莓派上搭建属于自己的网页(1)
Interface Test Framework Practice (4) | Get Schema Assertion
建立树形结构
C# async and multithreading
[Harmony OS] [ARK UI] ETS context basic operations
Super handy drawing tool is recommended
索引创建、删除与使用
接口管理工具YApi怎么用?颜值高、易管理、超好用
【Harmony OS】【ARK UI】Date 基本操作
接口测试框架实战(一) | Requests 与接口请求构造
Secondary development of WinForm controls
Coordinate knowledge in digital twin campus scenarios
Alienware上线首个数字时装AR试穿体验
链动2+1模式简单,奖励结构丰厚,自主裂变?
Get the Ip tool class
业务表解析-余额系统
GIS数据漫谈(六)— 投影坐标系统