当前位置:网站首页>Dynamically change the direction of this
Dynamically change the direction of this
2022-06-11 07:00:00 【Yu Mei】
this One of the most commonly heard words is : It always points to the object that invokes it , But it ignores this The environment
- this The point of is context dependent
- this The point can only be confirmed when the function is called , It is not certain when defining
- this The value of is an object
- In the overall situation , In non strict mode and strict mode this All point to the top-level object ( Browser is window).
1、call call
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
2、apply call
apply It's also a call function , You can change the inside of the function this The direction of , But his arguments must be arrays ( Pseudo array )
Application scenarios : It's often related to arrays
var o = {
name: 'andy'
}
function fn(a, b) {
console.log(this);
console.log(a+b)
};
fn()// At this time this Pointing to window The running result is 3
fn.apply(o,[1,2])// At this time this Point to the object o, Parameters are passed using an array The running result is 3
3、bind call
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 Subsequent production
New function generated
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 :
1、call and apply Will call functions , And change the inside of the function this Point to .
2、call and apply The parameters passed are different ,call Pass parameters separated by commas ,apply Pass... Using an array
3、bind Does not call function , You can change the inside of the function this Point to . - Application scenarios :
1、 call Often do inheritance .
2、 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
3、 bind Don't call functions , But also want to change this Point to . For example, change the internal timer this Point to .
边栏推荐
- [MATLAB image encryption and decryption] chaotic sequence image encryption and decryption (including correlation test) [including GUI source code 1862]
- [deploy private warehouse based on harbor] 3 deploy harbor
- Flutter 约束容器组件
- Summary of string processing skills II
- Reconstruction and preheating of linked list information management system (2) how to write the basic logic using linear discontinuous structure?
- .NET C#基础(6):命名空间 - 有名字的作用域
- Promise details
- Pytest automated test - easy tutorial (01)
- Mongodb installation
- RGB-D Salient Object Detection withCross-Modality Modulation and Selection
猜你喜欢

socket. IO cross domain stepping pit

Starting from scratch (V) realize bullet positioning and animation

Drawing with qpainter
![[Xunwei dry goods] opencv test of Godson 2k1000 development board](/img/94/312bb1f0d5e8d49506f659ad23cd3a.jpg)
[Xunwei dry goods] opencv test of Godson 2k1000 development board

Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。

Saltstack deployment LNMP
![JS implementation of Hill sort of graphic insertion sort [with source code]](/img/5a/b89fd5a0ebbc7ca17cc3e1fcb37c8f.jpg)
JS implementation of Hill sort of graphic insertion sort [with source code]

Won't virtual DOM be available in 2022? Introduction to virtual Dom and complete implementation of diff and patch

关于 QtCreator的设计器QtDesigner完全无法正常拽托控件 的解决方法

Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
随机推荐
Aircraft battle from scratch (III) flight between player aircraft and enemy aircraft
About daily report plan
572. subtree of another tree
Sohu employees encounter wage subsidy fraud. What is the difference between black property and gray property and how to trace the source?
Notice on organizing the application for the first edition of Ningbo key software in 2022
The realization of online Fox game server room configuration battle engagement customization function
Records how cookies are carried in cross domain requests
webserver
洛谷P1091合唱队形(最长上升子序列)
争议很大的问题
[matlab printed character recognition] OCR printed letter + number recognition [including source code 1861]
Do you use typescript or anyscript
The meaning and research significance of mathematical methodology
【Matlab图像融合】粒子群优化自适应多光谱图像融合【含源码 004期】
【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
sql查询问题,只显示列名 不显示数据
3.1 测试函数的命名规则
Shutter restraint container assembly
生物序列智能分析平台blog(1)
. Net C Foundation (6): namespace - scope with name