当前位置:网站首页>How to explain JS' bind simulation implementation to your girlfriend
How to explain JS' bind simulation implementation to your girlfriend
2022-07-29 00:56:00 【xswxsw2012】

episode
My girlfriend studies at home on weekends , Suddenly sent me a wechat message , Open it to see the simulation implementation bind, Let me tell her ~

Explain
Don't talk much , Start
First line of code
In function Function The prototype of the prototype Mount a bind2 Method
Function.prototype.bind2 = function(context) {}
Second line of code
hold this Assign a value to self, Mainly for the later implementation apply When ,self Pointing to bind2 The caller
var self = this;
Third line of code
Use Array.prototype.slice.call() Method to arguments This class array is from 1 Start , That is the first. 2 The parameters from item to tail are converted into an array , Some students may wonder why it is not the first 1 A start , Because the first item is usually an object , In the picture is context This into the reference , This input is at the end apply Time use In fact, the self and args Hereunder return When used in functions of, closures will be generated
var args = Array.prototype.slice.call(arguments, 1);
Fourth line of code
Returns a function
return function() {}
Line 5
Similar to the second line of code , Omitting the second input parameter here is actually from the first item to the end
var bindArgs = Array.prototype.slice.call(arguments);
The sixth line of code
perform apply Method change function this Point to context, The parameters of the function passed in twice are merged , Why merge parameters , because bind When used, the first return is a function , Execute this function again , It's comparable apply/call Use ; This also involves the reference of external variables by inner functions , That is often said closure .
self.apply(context, args.concat(bindArgs));
for instance :
var obj = {
type: 'car'
};
function factory(brand, color) {
console.log(' Commodity type :', this.type) // Commodity type : car
console.log(' Brand of goods :', brand) // Brand of goods : BMW
console.log(' The color of the goods :', color) // The color of the goods : red
}
var product = factory.bind2(obj, 'BMW');
product('red')
thus , End of explanation , My girlfriend said she understood , Continue to work hard
Little egg
Sort out the knowledge points :
Prototype prototype Mounting method this Point to Array.prototype.slice.call() Turn the class array into an array Closure apply Method
notes :bind If you are interested in the implementation of other scenarios, you can further explore .
边栏推荐
- 数仓搭建——ADS层
- The 30th day of question brushing
- Requestvideoframecallback() simple instance
- SQL Server 只有数据库文件,没有日志文件,恢复数据时报1813错误的解决方案
- 时间序列数据的预处理方法总结
- Main thread and daemon thread
- Data warehouse construction - ads floor
- Data warehouse construction - DWT floor
- 【commons-lang3专题】002- RandomUtils 专题
- "Food alliance ordering system"
猜你喜欢

Yield Guild Games:这一年的总结与未来展望

There is a span tag. If you want to do click events on it, how can you expand the click area

Summary of preprocessing methods for time series data

华为发布HarmonyOS 3.0,向“万物互联”再迈一步

17. Design of machine learning system

DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction

追踪伦敦银实时行情的方法有哪些?

Meeting notification & meeting feedback & feedback details function of meeting OA project

Educational Codeforces Round 132 (Rated for Div. 2)【A~C】

NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
随机推荐
Data warehouse construction - ads floor
MySQL的隔离级别、可能出现的问题(脏读、不可重复读、幻读)及其解决方法
Minimum dominating set (MDS) and its matlab code
[develop low code platform] low code rendering
B站“崩溃”始末 2021.07.13 我们是这样崩的(转载)
How to solve Oracle not available
Upload Excel files with El upload and download the returned files
CUDA相关
Data warehouse construction - DWT floor
Longest ascending subsequence
IMG tags prohibit dragging pictures
Some operations of Ubuntu remote server configuration database (unable to locate package MySQL server, steps of installing mysql, unable to enter password when logging in MySQL)
[network security] complete the blacklist and whitelist functions of server firewall through iptables and ipset
requestVideoFrameCallback() 简单实例
AQS principle
Execute immediate simple sample set (DML)
返回*this的成员函数
DRF - deserialization of serializer, fields and parameters, local and global hooks, use of modelserializer
mysql时间按小时格式化_mysql时间格式化,按时间段查询的MySQL语句[通俗易懂]
会议OA项目之会议通知&会议反馈&反馈详情功能