当前位置:网站首页>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 .
边栏推荐
- 管理区解耦架构见过吗?能帮客户搞定大难题的
- Api 接口优化的那些技巧
- Surfacecontrol and surfaceflinger communication
- UE4 common printing information methods for debugging
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
- Summary of preprocessing methods for time series data
- 多线程顺序运行的几种方法,面试可以随便问
- JWT token related configuration (global configuration identity authentication rewrites authenticate method)
- selenium对接代理与seleniumwire访问开发者工具NetWork
- 直流无刷电机控制器(换电机霍尔收费多少)
猜你喜欢
Outlier detection and Gan network (1)
Download the latest version of visual studio code and connect to the server remotely (very detailed)
[Yugong series] go teaching course in July 2022, an array of 020 go containers
Data warehouse construction - ads floor
数仓搭建——DWT层
Teach you how to install latex (nanny level tutorial)
Jupyter notebook中5个有趣的魔法命令
B- 树 ~
Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
关于ThreadPool的一些注意事项
随机推荐
Shell programming specifications and variables
我不建议你使用SELECT *
andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
requestVideoFrameCallback() 简单实例
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
I don't recommend you use Select*
Summary of preprocessing methods for time series data
Andriod6.0 low power mode (turn off WiFi, Bluetooth, GPS, screen brightness, etc.)
数学建模及其基础知识详解(化学常考知识点)
JWT token related configuration (global configuration identity authentication rewrites authenticate method)
17. Design of machine learning system
ORACLE not available如何解决
The 30th day of question brushing
Station B "crashed" from beginning to end 2021.07.13 we collapsed like this (Reprint)
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)
Requestvideoframecallback() simple instance
伦敦金即时行情带来什么机会?
Shell编程规范与变量
【无标题】