当前位置:网站首页>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 .
边栏推荐
- Mongodb features, differences with MySQL, and application scenarios
- Servlet overview
- 推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
- POI特效 市场调研
- Pytorch data input format requirements and conversion
- Moment.js
- TS basic data type
- Enabling partners, how can Amazon cloud technology "get on the horse and get a ride"?
- 2021-09-30
- Flight control implementation of four rotor aircraft "suggestions collection"
猜你喜欢

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

学习探索-波浪

电商RPA,大促轻松上阵的法宝

谷粒学苑P98踩坑 e.GlobalExceptionHandler : null

Secure code warrior learning record (III)

PHP JSON variable array problem

Grain Academy p98 trample pit e.globalexceptionhandler: null

Enabling partners, how can Amazon cloud technology "get on the horse and get a ride"?

Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download

Learning exploration-3d rotation card
随机推荐
The difference between MySQL clustered index and non clustered index
Source code of YY music wechat applet imitating Netease cloud music
Anaconda installation tutorial environment variables (how to configure environment variables)
Solution of phpstudy service environment 80 port occupied by process system under Windows
Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
生成随机数random学习之uniform_int_distribution,uniform_real_distribution
JS get the current date and time
Expression of directional signal -- complex exponential signal
@Import
类和对象(3)
js正则表达式匹配ip地址(ip地址正则表达式验证)
1913. Maximum product difference between two number pairs - no sorting required
Learning exploration - waves
日期类的实现
Drive board network cable directly connected to computer shared network configuration
谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
Serialize data type
@Autowired annotation required attribute
How does Navicat modify the language (Chinese or English)?