当前位置:网站首页>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 !!
边栏推荐
- Cross-entrpy Method
- Compilation of kickstart file
- Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
- The difference between redirectto and navigateto in uniapp
- 准备好在CI/CD中自动化持续部署了吗?
- JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
- 数据运营平台-数据采集[通俗易懂]
- 【CVPR 2022】半监督目标检测:Dense Learning based Semi-Supervised Object Detection
- [CVPR 2022] semi supervised object detection: dense learning based semi supervised object detection
- Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
猜你喜欢

专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统

基于SSM框架的文章管理系统

Introduction au GPIO

System activity monitor ISTAT menus 6.61 (1185) Chinese repair

【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)

37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme

互动滑轨屏演示能为企业展厅带来什么

Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database

37 page overall planning and construction plan for digital Village revitalization of smart agriculture

Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing
随机推荐
Leecode brush questions record interview questions 32 - I. print binary tree from top to bottom
一图看懂对程序员的误解:西方程序员眼中的中国程序员
System activity monitor ISTAT menus 6.61 (1185) Chinese repair
Operation test of function test basis
TypeScript中使用类型别名
48页数字政府智慧政务一网通办解决方案
互动滑轨屏演示能为企业展厅带来什么
Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
Interesting wine culture
MIT 6.824 - raft Student Guide
C语言输入/输出流和文件操作【二】
微信小程序uploadfile服务器,微信小程序之wx.uploadFile[通俗易懂]
rancher集成ldap,实现统一账号登录
DAY SIX
【vulnhub】presidential1
PostgreSQL uses pgpool II to realize read-write separation + load balancing
2022/2/10 summary
《LaTex》LaTex数学公式简介「建议收藏」
Encryption algorithm - password security