当前位置:网站首页>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
边栏推荐
- QT based travel query and simulation system
- Create servlet project
- 視頻分類的類間和類內關系——正則化
- 如何确定首页和搜索之间的关系呢?首页与搜索的关系
- Channel Shuffle类
- Compiling Draco library on Windows platform
- Judge whether the network file exists, obtain the network file size, creation time and modification time
- System.IO.FileLoadException异常
- Blue Bridge Cup 2015 CA provincial competition (filling the pit)
- How to select standard products and non-standard products, the importance of selection, and how to layout the store
猜你喜欢

Asynchronous path processing

QML学习 第一天

Common debugging tools and commands for ROS

Open source project - (erp+ Hotel + e-commerce) background management system

A. Prefix range

IP address management

Design of secure chat tool based on C #

TinyMCE realizes automatic uploading of pasted pictures

Ros- resolve error "tf2\u buffer\was not declared in this scope"

TinyMCE series (II) TinyMCE plug-in development
随机推荐
【深度学习基础】反向传播法(1)
The second day of QML study
B.刷墙(C语言)
服务端渲染与客户端渲染的区别(优缺点)
机器学习之决策树
[foundation of deep learning] back propagation method (1)
【QNX Hypervisor 2.2 用户手册】4.1 构建QNX Hypervisor系统的方法
5G NR协议学习--TS38.211下行通道
UML series articles (31) architecture modeling - deployment diagram
QML first day
点击产生4位随机数,验证码设置
Load/store instruction addressing mode of arm instruction set (2)
文件夹目录结构自动生成
How to select standard products and non-standard products, the importance of selection, and how to layout the store
Common debugging tools and commands for ROS
为什么新品发布上架之后会没有流量,新品应该怎么发布?
Linear model of machine learning
ARM指令集之乘法指令
TinyMCE series (III) introduction to common TinyMCE APIs
ARM指令集之批量Load/Store指令