当前位置:网站首页>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;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复。
边栏推荐
- 好消息!北京、珠海PMP考试时间来啦
- 5.回顾简单的神经网络
- 【Biotin Azide|cas:908007-17-0】Price_Manufacturer
- [Harmony OS] [ARK UI] ETS context basic operations
- 【精讲】利用原生js实现todolist
- 普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
- Super handy drawing tool is recommended
- Talking about GIS Data (6) - Projected Coordinate System
- Redis缓存雪崩、缓存穿透、缓存击穿
- 自组织是管理者和成员的双向奔赴
猜你喜欢

Concepts and Methods of Exploratory Testing

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

Windows 安装PostgreSQL

typescript49-交叉类型

Talking about GIS Data (6) - Projected Coordinate System

Shell条件语句判断

Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG

社交电商如何做粉丝运营?云平台怎么选择商业模式?

IO process thread -> thread -> day5

测试人员的价值体现在哪里
随机推荐
Two ways to simulate multi-user login in Jmeter
接口测试实战| GET/POST 请求区别详解
Interface test practice | Detailed explanation of the difference between GET / POST requests
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
MySQL 删除表数据,重置自增 id 为 0 的两个方式
Redis连接不上的报错解决方案汇总
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
7.Keras开发简介
WebSocket的实际应用
Practical application of WebSocket
js garbage collection mechanism
DFS's complement to pruning
【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
索引创建、删除与使用
[Harmony OS] [ArkUI] ets development graphics and animation drawing
接口和抽象
install ambari
Super handy drawing tool is recommended
js中的闭包
数字化时代,企业如何建立自身的云平台与商业模式的选择?