当前位置:网站首页>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 !!
边栏推荐
- Leecode brush question record sword finger offer 58 - ii Rotate string left
- System activity monitor ISTAT menus 6.61 (1185) Chinese repair
- Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system
- Quaternion attitude calculation of madgwick
- AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
- 华为mate8电池价格_华为mate8换电池后充电巨慢
- C语言输入/输出流和文件操作【二】
- Clipboard management tool paste Chinese version
- Everyone is always talking about EQ, so what is EQ?
- 智能运维应用之道,告别企业数字化转型危机
猜你喜欢

陀螺仪的工作原理

MySQL learning notes (mind map)

X.509 certificate based on go language

Mujoco Jacobi - inverse motion - sensor

System activity monitor ISTAT menus 6.61 (1185) Chinese repair

Mujoco second order simple pendulum modeling and control

2022年PMP项目管理考试敏捷知识点(9)

The difference between redirectto and navigateto in uniapp

英雄联盟|王者|穿越火线 bgm AI配乐大赛分享

AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
随机推荐
Notes of training courses selected by Massey school
ZYNQ移植uCOSIII
Leecode brushes questions to record interview questions 17.16 massagist
AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
TypeScript增量编译
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
什么是响应式对象?响应式对象的创建过程?
英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
The difference between redirectto and navigateto in uniapp
Business process testing based on functional testing
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
File and image comparison tool kaleidoscope latest download
Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
QT tutorial: creating the first QT program
Clipboard management tool paste Chinese version
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
Leecode brush questions record interview questions 32 - I. print binary tree from top to bottom
Command line kills window process
2021 SASE integration strategic roadmap (I)
如何判断一个数组中的元素包含一个对象的所有属性值