当前位置:网站首页>This point inside the function / change this point inside the function
This point inside the function / change this point inside the function
2022-07-25 23:27:00 【Dragon eyes】
intra-function this Point to
these this The direction of , It's determined when we call the function . The different calling methods determine this The direction of is different
Generally point to our caller .
Change function interior this Point to
call Method
call() Method calls an object . Simply understood as the way to call a function , But it can change the function this Point to
Application scenarios : Often do inheritance .
var o = {
name: 'andy'
}
function fn(a, b) {
console.log(this);
console.log(a+b)
};
fn(1,2)// At this time this Pointing to window The running result is 3
fn.call(o,1,2)// At this time this Point to the object o, Parameters are separated by commas , The running result is 3
The result of the above code is :
bind Method
bind() Methods do not call functions , But it can change the inside of the function this Point to , Return is the original function change this And the new functions that come out of it
If you just want to change this Point to , And when you don't want to call this function , have access to bind
Application scenarios : Don't call functions , But also want to change this Point to
var o = {
name: 'andy'
};
function fn(a, b) {
console.log(this);
console.log(a + b);
};
var f = fn.bind(o, 1, 2); // Here f yes bind New function returned
f();// Call new function this Point to the object o Parameters are separated by commas

call、apply、bind The similarities and differences of the three
Common ground : Can change this Point to
Difference :
- call and apply Will call functions , And change the inside of the function this Point to .
- call and apply The parameters passed are different ,call Pass parameters separated by commas ,apply Pass... Using an array
- bind Does not call function , You can change the inside of the function this Point to .
Application scenarios
- call Often do inheritance .
- apply It's often related to arrays . For example, with the help of mathematical objects to achieve the maximum and minimum value of the array
- bind Don't call functions , But also want to change this Point to . For example, change the internal timer this Point to .
边栏推荐
- Family relationship calculator wechat applet source code
- [QNX Hypervisor 2.2用户手册]9.8 load
- Rendering, filtering (filtering) and sorting of lists
- Summary of built-in instructions and custom instructions
- MVVM model
- ASP date function (what if the disk function is incorrect)
- 类和对象(3)
- Several commonly used traversal methods
- ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
- Which securities firm is the best and safest for beginners to open an account
猜你喜欢

POI special effects Market Research

Tips for using (1)

File contains vulnerability
![Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []](/img/06/85a6ba450fc2637a4ac1cf6a630912.png)
Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []

PHP JSON variable array problem

WordPress removes the website publishing time

initializer_list工具库学习

Family relationship calculator wechat applet source code

Tencent map API request source is not authorized, this request source domain name

Source code of YY music wechat applet imitating Netease cloud music
随机推荐
ASP date function (what if the disk function is incorrect)
Tips for using (1)
Data broker understanding
Custom MVC principle
Flight control implementation of four rotor aircraft "suggestions collection"
电商RPA,大促轻松上阵的法宝
[QNX Hypervisor 2.2用户手册]9.8 load
生成随机数random学习之uniform_int_distribution,uniform_real_distribution
The new UI people help task help PHP source code with a value of 1500 / reward task Tiktok Kwai headline like source code / with three-level distribution can be packaged applet
TS interface
XXE&XML-外部实体注入-利用和绕过
多模态——Deep Multi-Modal Sets
Secure code warrior learning record (IV)
Redis过期键的删除策略[通俗易懂]
What has Amazon cloud technology done right to become the leader of cloud AI services for three consecutive years?
PHP binary array is sorted by a field in it
策略模式_
Classes and objects (3)
四旋翼飞行器的飞控实现「建议收藏」
What is a physical firewall? What's the effect?