当前位置:网站首页>The difference between bind, call and apply, and the encapsulation of bind()
The difference between bind, call and apply, and the encapsulation of bind()
2022-06-12 12:05:00 【Mustang (Mustang)】
1.bind、call、apply Differences among the three :
1)bind The return value of is a function body , Will not be called immediately
2)call、apply Will immediately call , The first parameter is used to change this The direction of , The difference between the two is that the former passes parameters one by one , The latter is passed as an array
3)bind、call、apply These three methods are all functions (Function) Methods
2.bind Encapsulation :
bind Characteristics :(1) Returns the body of a function (2) change this The direction of (3) Pass parameters
Original bind Method can be implemented :(1)this Changes (2) You can inherit your own prototype methods (3)new After oneself this Pointing to the constructor is itself ( Constructor's this Point to instance object )
Function.prototype.bindTo = function(){
// The first parameter passed , That is, the this Point to ( Use arguments Formal parameter copy to receive all parameters )
var that = arguments[0];
// Get the remaining parameters
//slice The method is to split the array , Parameter is 1 Is for splitting from the second item of the array , Until the last item of the array
var arr = Array.prototype.slice.call(arguments).slice(1);
var _this = this;// Current this Point to bindTo This function
var fn = function(){
// Point current to bindTo Functional this To change ,that Indicates what needs to be changed this Point to , adopt apply Methods to change
//arr For other parameters that need to be passed
//this instanceof fn What is judged is whether it is currently new too , If new After that , Now this Should point to the constructor itself ( Constructor's this Point to the instantiated object ), and this Just point to the current bindTo function ; without new too , Now this It should point to the passed parameter ( Implement change this The direction of )
var newThis = this instanceof fn?this:that;
_this.apply(newThis ,arr);
}
// Implementation inheritance
fn.prototype = {
constructor:fn,
__proto__:this.prototype
}
return fn;
}
perform :
function Fn(a,b,c){
console.log(this,a,b,c);
}
Fn.prototype = {
show(){}
}
var fn1 = Fn.bindTo(document);// The original this It's pointing window Of , In the use of the bindTo Method can be used to this Point to document
var p = new fn1();//new After that, I will this Point to Fn(), The constructor itself ( Constructor's this Point to instance object )
console.log(p);// Print p You can inherit show() Prototype method
边栏推荐
- 视频分类的类间和类内关系——正则化
- Click to generate 4-bit random number and verify code setting
- 用cloneNode 克隆,解决id问题/方法 深复制和浅复制修改id的方法
- Ficusjs series (I) introduction to ficusjs
- Automatic generation of folder directory structure
- Neighbor item status update of neighbor subsystem
- Elk construction guide
- 【Leetcode】416. Split equal sum subset
- Data processing instructions of arm instruction set
- System. IO. Fileloadexception exception
猜你喜欢

Analyze the implementation principle of the onion model, and connect the onion model in your own project

LeetCode 497. 非重叠矩形中的随机点(前缀和+二分)

Lambda and filter, index of list and numpy array, as well as various distance metrics, concatenated array and distinction between axis=0 and axis=1

PDSCH 相关

异步路径处理

5g NR Protocol Learning - - ts38.211 downlink channel

【Leetcode】79. Word search

LeetCode 1037. 有效的回旋镖(向量叉乘)

转载--win10打开任务管理器就蓝屏的问题

LeetCode 890. Find and replace mode (analog + double hash table)
随机推荐
In navigation, the solution of adding borders to affect the layout
5g NR protocol learning -- ts38.211 downlink channel
Multiplication instruction of arm instruction set
影响店铺权重的三十一条,快来看看中招了吗
Spark common encapsulation classes
机器学习之决策树
点击产生4位随机数,验证码设置
【Leetcode】199. Right view of binary tree
Channel shuffle class
SSL引入原因及加密步骤
ARM指令集之伪指令
左右案例+小圆点的轮播图+无时间
this的指向
Chaîne la plus longue sans caractères dupliqués (leetcode 3)
淘宝新改版商家如何操作,需要注意的点有哪些
导航中,添加边框影响布局的解决方法
[foundation of deep learning] back propagation method (1)
[foundation of deep learning] learning of neural network (4)
创建Servlet项目
Load/store access instruction of arm instruction set (2)