当前位置:网站首页>Explain in detail the implementation of call, apply and bind in JS (source code implementation)
Explain in detail the implementation of call, apply and bind in JS (source code implementation)
2022-07-07 00:35:00 【Cut potatoes into shreds】
stay JavaScript in ,call、apply and bind yes Function Object comes with three methods , The main purpose of these three methods is to change the this Point to .
call、apply、bind Common ground of methods :
1. It's all about changing functions this Object's pointing to ;
2. The first parameter is this Object to point to ;
3. You can use subsequent parameters to transfer parameters ;
difference :
bind Yes returns the corresponding function , Easy to call later ;apply 、call Call immediately .
call() Methods and apply() The method works the same , The only difference is the way parameters are accepted .
One 、call Source code implementation
//call The source code to achieve
function demo(){
console.log(this);
console.log(arguments);
}
var obj = {
neme:"lcy",
age:2
}
Function.prototype.myCall = function(){
// Find the first parameter , Determine whether the first parameter is empty , If it is empty this Or point to window
var target = arguments[0] || window// Short-circuit operation
// Get the parameters passed
var _arg = Array.from(arguments).slice(1)
target.fn = this;
target.fn(..._arg);
// Delete fn attribute
delete target.fn;
}
demo.myCall(obj,1,2,3)
// demo.call(obj,1,2,3);
Two 、apply Source code implementation
//apply The source code to achieve
function demo(){
console.log(this);
console.log(arguments);
}
var obj = {
neme:"lcy",
age:2
}
Function.prototype.myApply = function(){
// Find the first parameter , Determine whether the first parameter is empty , If it is empty this Or point to window
var target = arguments[0] || window// Short-circuit operation
// Get the parameters passed
var _arg = arguments[1]
target.fn = this;
target.fn(..._arg);
// Delete fn attribute
delete target.fn;
}
demo.myApply(obj,[1,3,3])
// demo.apply(obj,[1,2,3]);
3、 ... and 、bind Source code implementation
// bind The source code to achieve
function demo(){
console.log(this);
console.log(arguments);
}
var obj = {
neme:"lcy",
age:2
}
Function.prototype.myBind = function(){
// Record function's this Point to
var target = arguments[0] || window;
// Get the parameters passed
var _args = Array.from(arguments).slice(1);
// Record calls bind Function of
var that = this;
// Returns a new function
return function(){
if(!new.target){
var arg = Array.from(arguments);
// Splicing parameter
arg = _args.concat(arg);
target.fn = that;
target.fn(...arg);
delete target.fn;
}else{
return new that();
}
}
}
var fn = demo.myBind(obj,1,2);
fn(3,4);
// var fn1 = demo.bind(obj,1,2);
// fn1(3,4)
This is recorded for future use , I also hope the big guys can communicate more , Leave more messages , Point out my shortcomings !
You can study it if you need it !!
边栏推荐
- File and image comparison tool kaleidoscope latest download
- Encryption algorithm - password security
- Markov decision process
- 工程师如何对待开源 --- 一个老工程师的肺腑之言
- Pinia module division
- Supersocket 1.6 creates a simple socket server with message length in the header
- rancher集成ldap,实现统一账号登录
- GPIO简介
- 【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
- Everyone is always talking about EQ, so what is EQ?
猜你喜欢
三维扫描体数据的VTK体绘制程序设计
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
Are you ready to automate continuous deployment in ci/cd?
System activity monitor ISTAT menus 6.61 (1185) Chinese repair
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
The difference between redirectto and navigateto in uniapp
Mujoco finite state machine and trajectory tracking
Racher integrates LDAP to realize unified account login
Clipboard management tool paste Chinese version
After leaving a foreign company, I know what respect and compliance are
随机推荐
How to set encoding in idea
Markov decision process
C language input / output stream and file operation [II]
Leecode brush questions record interview questions 32 - I. print binary tree from top to bottom
509 certificat basé sur Go
dynamic programming
Business process testing based on functional testing
DAY THREE
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
[CVPR 2022] target detection sota:dino: Detr with improved detecting anchor boxes for end to end object detection
GPIO简介
Mujoco second order simple pendulum modeling and control
如何判断一个数组中的元素包含一个对象的所有属性值
Operation test of function test basis
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
Random类的那些事
Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
Model-Free Prediction
Typescript incremental compilation