当前位置:网站首页>bind、call、apply三者的区别,还有bind()的封装
bind、call、apply三者的区别,还有bind()的封装
2022-06-12 11:58:00 【mustang(野马)】
1.bind、call、apply三者的区别:
1)bind的返回值是一个函数体,不会被立即调用
2)call、apply会立即调用,第一个参数都是用来改变this的指向,两者的区别是前者传递参数的时候是一个一个传递,后者是以一个数组的形式传递
3)bind、call、apply这三个方法都属于函数(Function)的方法
2.bind的封装:
bind的特点:(1)返回一个函数体(2)改变this的指向(3)传递参数
原装的bind方法可以实现:(1)this的改变(2)可以继承自身的原型方法(3)new自身之后this指向构造函数也就是自身(构造函数的this指向实例对象)
Function.prototype.bindTo = function(){
//传递过来的第一个参数,即需要改变的this指向(使用arguments形参副本来接收所有的参数)
var that = arguments[0];
//获取到剩余的参数
//slice方法是将数组进行分割,参数为1是代表从数组的第二项开始分割,一直到数组的最后一项
var arr = Array.prototype.slice.call(arguments).slice(1);
var _this = this;//当前的this指向bindTo这个函数
var fn = function(){
//将当前指向bindTo函数的this进行改变,that表示需要更改的this指向,通过apply方法来改变
//arr为需要传递的其他参数
//this instanceof fn判断的是当前是否被new过,如果new过了,则此时this应该指向构造函数本身(构造函数的this指向实例化对象),而this正好指向当前的bindTo函数;如果没有new过,则此时this应该指向传递过来的参数(实现改变this的指向)
var newThis = this instanceof fn?this:that;
_this.apply(newThis ,arr);
}
//实现继承
fn.prototype = {
constructor:fn,
__proto__:this.prototype
}
return fn;
}
执行:
function Fn(a,b,c){
console.log(this,a,b,c);
}
Fn.prototype = {
show(){}
}
var fn1 = Fn.bindTo(document);//原来的this是指向window的,在使用了bindTo方法后可以将this指向document
var p = new fn1();//new了之后会将this指向Fn(),即构造函数自身(构造函数的this指向实例对象)
console.log(p);//打印p后可以继承show()原型方法
边栏推荐
- 为什么新品发布上架之后会没有流量,新品应该怎么发布?
- QML学习 第二天
- Deep learning and CV tutorial (14) | image segmentation (FCN, segnet, u-net, pspnet, deeplab, refinenet)
- ARM指令集之数据处理类指令
- Kubernetes cluster setup
- Blue Bridge Cup 2015 CA provincial competition (filling the pit)
- Analyze the implementation principle of the onion model, and connect the onion model in your own project
- LeetCode 1037. Effective boomerang (vector cross product)
- Load/store memory access instruction of arm instruction set (1)
- 一个人必须不停地写作,才能不被茫茫人海淹没。
猜你喜欢

IP address management

Inter class and intra class relations in video classification -- regularization

Doris records service interface calls

Design of virtual scrolling list

QT adds a summary of the problems encountered in the QObject class (you want to use signals and slots) and solves them in person. Error: undefined reference to `vtable for xxxxx (your class name)‘

PDSCH 相关

First understand the onion model, analyze the implementation process of middleware, and analyze the source code of KOA Middleware

6.6 Convolution de séparation

MySQL - built in function

6.6 分离卷积
随机推荐
K53. Chapter 2 installing kubernetes v1.22 based on binary packages -- cluster deployment
Load/store instruction addressing mode of arm instruction set (1)
Why is there no traffic after the launch of new products? How should new products be released?
Basic concepts of machine learning
Longest string without duplicate characters (leetcode 3)
Basic principle of Doppler effect
Cookies and sessions
LeetCode_二分搜索_中等_162. 寻找峰值
ARM指令集之数据处理类指令
Socket implements TCP communication flow
Data processing instruction addressing method of arm instruction set
Design of secure chat tool based on C #
Spark常用封装类
标品和非标品如何选品,选品的重要性,店铺怎样布局
kubernetes集群搭建
影响店铺权重的三十一条,快来看看中招了吗
UML series articles (30) architecture modeling -- product diagram
System. IO. Fileloadexception exception
Architecture training module 7
ARM指令集之伪指令