当前位置:网站首页>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 .
边栏推荐
- CUDA相关
- DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use
- seleniumwire获取百度指数
- The method of tracking the real-time market of London Silver
- day8
- AQS principle
- 17. Design of machine learning system
- Several methods of multi-threaded sequential operation can be asked casually in the interview
- redis版本怎么查看(查看redis进程)
- 云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
猜你喜欢
![Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]](/img/e3/496247afdb3ea5b9a9cdb8afb0d41b.png)
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]

自制 | 纯手工自制一个16位RISC架构CPU

Nftscan and nftplay have reached strategic cooperation in the field of NFT data

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告

浅谈一下跨端技术方案

Outlier detection and Gan network (1)

Error reporting: when the browser clicks the modify add button, there is no response and no error reporting. Solution

Api 接口优化的那些技巧
![[Yugong series] go teaching course in July 2022, an array of 020 go containers](/img/06/b2f69599b30c4a93a6240613cbee84.png)
[Yugong series] go teaching course in July 2022, an array of 020 go containers

Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
随机推荐
保护性拷贝&无状态
【commons-lang3专题】002- RandomUtils 专题
mysql时间按小时格式化_mysql时间格式化,按时间段查询的MySQL语句[通俗易懂]
SurfaceControl和SurfaceFlinger通信
The download file of native JS implementation can be used anywhere
Selenium wire obtains Baidu Index
【MySQL 8】Generated Invisible Primary Keys(GIPK)
Asynchronous mode worker thread
DRF - deserialization of serializer, fields and parameters, local and global hooks, use of modelserializer
rk3399 9.0驱动添加Powser按键
17. Design of machine learning system
There is a span tag. If you want to do click events on it, how can you expand the click area
Data warehouse construction - ads floor
Android必备的面试技能(含面试题和学习资料)
浅谈一下跨端技术方案
Tips for API interface optimization
Data warehouse construction - DWT floor
CUDA related
多线程顺序运行的几种方法,面试可以随便问
ORACLE not available如何解决