当前位置:网站首页>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 !!
边栏推荐
- Quaternion attitude calculation of madgwick
- Data analysis course notes (V) common statistical methods, data and spelling, index and composite index
- DAY FIVE
- Google, Baidu and Yahoo are general search engines developed by Chinese companies_ Baidu search engine URL
- 37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
- 智能运维应用之道,告别企业数字化转型危机
- 2021 SASE integration strategic roadmap (I)
- 刘永鑫报告|微生物组数据分析与科学传播(晚7点半)
- VTK volume rendering program design of 3D scanned volume data
- The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
猜你喜欢

alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况

Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools

如何判断一个数组中的元素包含一个对象的所有属性值

Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis

If the college entrance examination goes well, I'm already graying out at the construction site at the moment

Core knowledge of distributed cache

Data analysis course notes (V) common statistical methods, data and spelling, index and composite index

沉浸式投影在线下展示中的三大应用特点

Mujoco second order simple pendulum modeling and control

iMeta | 华南农大陈程杰/夏瑞等发布TBtools构造Circos图的简单方法
随机推荐
Leecode brush questions record interview questions 32 - I. print binary tree from top to bottom
Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
How engineers treat open source -- the heartfelt words of an old engineer
Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
dynamic programming
vector的使用方法_vector指针如何使用
Introduction au GPIO
GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
陀螺仪的工作原理
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
基於GO語言實現的X.509證書
工程师如何对待开源 --- 一个老工程师的肺腑之言
Quaternion attitude calculation of madgwick
2022/2/12 summary
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
GPIO簡介
Use source code compilation to install postgresql13.3 database
stm32F407-------DAC数模转换
Leecode brush question record sword finger offer 58 - ii Rotate string left