当前位置:网站首页>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;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复.
边栏推荐
- 在树莓派上搭建属于自己的网页(1)
- shell script loop statement
- 接口管理工具YApi怎么用?颜值高、易管理、超好用
- Interface Test Framework Practice (4) | Get Schema Assertion
- Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
- How to prepare for the test interface test data
- 【Harmony OS】【FAQ】Hongmeng Questions Collection 1
- Shell条件语句判断
- 接口和协议
- 数据库基本概述与SQL概述
猜你喜欢

MCM箱模型建模方法及大气O3来源解析

Talking about GIS Data (5) - Geographic Coordinate System

接口和协议

【Harmony OS】【ARK UI】轻量级数据存储

【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接

力扣561. 数组拆分

CobalStrike(CS)基础超级详细版

Secondary development of WinForm controls

Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8

Talking about GIS Data (6) - Projected Coordinate System
随机推荐
How to prepare for the test interface test data
【 Harmony OS 】 【 ano UI 】 lightweight data storage
Exception(异常) 和 Error(错误)区别解析
用户密码加密工具
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
超好用的画图工具推荐
GIS数据漫谈(六)— 投影坐标系统
数据库基本概述与SQL概述
[Fine talk] Using native js to implement todolist
[Harmony OS] [ArkUI] ets development graphics and animation drawing
C# async and multithreading
Practical application of WebSocket
Jmeter 模拟多用户登录的两种方法
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
【Harmony OS】【ARK UI】Date 基本操作
js garbage collection mechanism
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
【Harmony OS】【FAQ】鸿蒙问题合集1
MySQL 出现 The table is full 的解决方法
c语言结构体中的冒泡排序