当前位置:网站首页>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;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复。
边栏推荐
- Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
- BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
- Secondary development of WinForm controls
- 2.何为张量
- Shell之条件语句
- Flink state
- typescript41-class类的私有修饰符
- 如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
- 设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
- 13.机器学习基础:数据预处理与特征工程
猜你喜欢

设计模式——组合模式、享元模式(Integer缓存)(结构型模式)

12.机器学习基础:评估机器学习模型

Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data

数字孪生园区场景中的坐标知识

redis键值出现 xacxedx00x05tx00&的解决方法

链动2+1模式简单,奖励结构丰厚,自主裂变?

接口和抽象

超好用的画图工具推荐

2022/08/02 Study Notes (day22) Multithreading

Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
随机推荐
CobalStrike(CS)基础超级详细版
JS bottom handwriting
Kotlin-Flow common encapsulation class: the use of StateFlow
js的垃圾回收机制
【Biotin Azide|cas:908007-17-0】Price_Manufacturer
私域流量时代来临,电商企业如何布局?
typescript45-接口之间的兼容性
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
在线密码生成工具推荐
接口测试实战| GET/POST 请求区别详解
如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
typescript49-交叉类型
MySQL 删除表数据,重置自增 id 为 0 的两个方式
MySQL 出现 The table is full 的解决方法
Alienware上线首个数字时装AR试穿体验
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
Flink state
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
移动流量的爆发式增长,社交电商如何选择商业模式
接口测试框架实战(二)| 接口请求断言