当前位置:网站首页>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;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复.
边栏推荐
- 【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
- MySQL 删除表数据,重置自增 id 为 0 的两个方式
- Apache DolphinScheduler版本2.0.5分布式集群的安装
- 在树莓派上搭建属于自己的网页(1)
- How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
- Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
- 【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
- 软件开发的最大的区别是什么?
- 测试人员的价值体现在哪里
- IO进程线程->线程->day5
猜你喜欢

WinForm的控件二次开发

在树莓派上搭建属于自己的网页(1)

Interface Test Framework Practice (4) | Get Schema Assertion

在树莓派上搭建属于自己的网页(2)

阿里云对象存储oss私有桶生成链接

Windows 安装PostgreSQL

2022/08/02 学习笔记 (day22) 多线程

Detailed explanation of MOSN reverse channel

MCM box model modeling method and source analysis of atmospheric O3
![[Harmony OS] [ARK UI] ETS context basic operations](/img/40/d5924477c42e2b3246eb212f4be534.png)
[Harmony OS] [ARK UI] ETS context basic operations
随机推荐
【Harmony OS】【ARK UI】ETS 上下文基本操作
【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
typescript41-class类的私有修饰符
[Harmony OS] [ARK UI] ETS context basic operations
【Harmony OS】【FAQ】鸿蒙问题合集1
Redis连接不上的报错解决方案汇总
表的创建、修改与删除
Create a tree structure
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
rosbag工具plotjuggler无法打开rosbag的问题
js garbage collection mechanism
Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures
私域流量时代来临,电商企业如何布局?
MCM箱模型建模方法及大气O3来源解析
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
【精讲】利用原生js实现todolist
接口测试框架实战(二)| 接口请求断言
【开发者必看】【push kit】推送服务服务典型问题合集2
用户密码加密工具
JS底层手写