当前位置:网站首页>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 .
边栏推荐
- BI 系统中为什么会有很多快照表?
- Solution of phpstudy service environment 80 port occupied by process system under Windows
- TS basic data type
- Simulate and implement common interfaces of string class
- Summary of common PHP functions
- PHP JSON variable array problem
- Tencent map API request source is not authorized, this request source domain name
- MES系统设备管理概述(下)
- PHP binary array is sorted by a field in it
- MVVM model
猜你喜欢

Very simple vsplayaudio online music player plug-in

The VM session was closed before any attempt to power it on

serialization and deserialization

类和对象(3)

Dynamic memory management

Secure code warrior learning record (IV)

学习探索-3d轮播卡片

Several commonly used traversal methods

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

Apple CMS V10 template /mxone Pro adaptive film and television website template
随机推荐
Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
新手哪个券商开户最好 开户最安全
新手开户选择哪个券商公司好呢?安全吗
How does Navicat modify the language (Chinese or English)?
Recommended system - an embedded learning framework for numerical features in CTR prediction
模拟实现string类常用接口
BI 系统中为什么会有很多快照表?
PHP JSON variable array problem
Mongodb features, differences with MySQL, and application scenarios
Anaconda installation tutorial environment variables (how to configure environment variables)
Call Gaode map -- address is converted into longitude and latitude
[QNX Hypervisor 2.2用户手册]9.8 load
@Autowired注解 required属性
Scaffold installation
5 ROS simulation modeling (3-rviz+gazebo+ control simulation robot)
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
生成随机数random学习之uniform_int_distribution,uniform_real_distribution
General paging function
动态内存管理