当前位置:网站首页>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 !!
边栏推荐
- JS import excel & Export Excel
- 基于SSM框架的文章管理系统
- dynamic programming
- Jenkins' user credentials plug-in installation
- 华为mate8电池价格_华为mate8换电池后充电巨慢
- Mujoco second order simple pendulum modeling and control
- 三维扫描体数据的VTK体绘制程序设计
- Oracle EMCC 13.5 environment in docker every minute
- alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
- 数据运营平台-数据采集[通俗易懂]
猜你喜欢

准备好在CI/CD中自动化持续部署了吗?

陀螺仪的工作原理

How to judge whether an element in an array contains all attribute values of an object

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

After leaving a foreign company, I know what respect and compliance are

Mujoco finite state machine and trajectory tracking

Testers, how to prepare test data

Business process testing based on functional testing

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

AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
随机推荐
[boutique] Pinia Persistence Based on the plug-in Pinia plugin persist
MIT 6.824 - raft Student Guide
St table
互动滑轨屏演示能为企业展厅带来什么
Everyone is always talking about EQ, so what is EQ?
Encryption algorithm - password security
PXE server configuration
2022年PMP项目管理考试敏捷知识点(9)
QT tutorial: creating the first QT program
A way of writing SQL, update when matching, or insert
Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
VTK volume rendering program design of 3D scanned volume data
Advanced learning of MySQL -- basics -- multi table query -- inner join
数据运营平台-数据采集[通俗易懂]
微信小程序uploadfile服务器,微信小程序之wx.uploadFile[通俗易懂]
基於GO語言實現的X.509證書
ZYNQ移植uCOSIII
Idea automatically imports and deletes package settings
[CVPR 2022] semi supervised object detection: dense learning based semi supervised object detection