当前位置:网站首页>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;
}文章推荐:
感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复。
边栏推荐
- Redis缓存雪崩、缓存穿透、缓存击穿
- typescript41-class类的私有修饰符
- Redis连接不上的报错解决方案汇总
- Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
- typescript42-readonly修饰符
- UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
- 【Harmony OS】【FAQ】鸿蒙问题合集1
- typescript43-类型兼容性说明
- StarRocks July Community Update
- 【Harmony OS】【ARK UI】轻量级数据存储
猜你喜欢
![[Harmony OS] [ArkUI] ets development graphics and animation drawing](/img/36/f4c91f794b1321f11a24505d1617fb.png)
[Harmony OS] [ArkUI] ets development graphics and animation drawing

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

typescript45-接口之间的兼容性

9.新闻分类:多分类问题

shell脚本循环语句

【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
![[Harmony OS] [ARK UI] ETS context basic operations](/img/40/d5924477c42e2b3246eb212f4be534.png)
[Harmony OS] [ARK UI] ETS context basic operations

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

接口测试框架实战 | 流程封装与基于加密接口的测试用例设计

DFS对剪枝的补充
随机推荐
typescript43-类型兼容性说明
表的创建、修改与删除
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
接口测试框架实战(四)| 搞定 Schema 断言
接口测试如何准备测试数据
数据库基本概述与SQL概述
接口管理工具YApi怎么用?颜值高、易管理、超好用
BIOTIN ALKYNE CAS:773888-45-2价格,供应商
探索性测试的概念及方法
Talking about GIS Data (5) - Geographic Coordinate System
DDL操作数据库、表、列
JS底层手写
建立树形结构
DFS对剪枝的补充
10.预测房价:回归问题
6.神经网络剖析
【Biotin Azide|cas:908007-17-0】Price_Manufacturer
[Developers must see] [push kit] Collection of typical problems of push service service 2
3.张量运算
【开发者必看】【push kit】推送服务服务典型问题合集2