当前位置:网站首页>How to change the direction of this gracefully
How to change the direction of this gracefully
2022-07-24 03:57:00 【InfoQ】
Preface
bind()call()apply()this Point to
change this Three functions of
call()
call()thisgrammar
fun.call(thisArg, arg1, arg2, ...)- thisArg: stay fun Specified by function runtime this value
- args1, arg2, ... : Other parameters passed
- The return value is the return value of the function , Because it's the calling function
var ldh = {
name: ' Lau Andy '
}
function f(a, b) {
console.log(this); //ldh
console.log(a + b); //5
}
f.call(ldh, 2, 3);effect
- callThe first one can call the function The second one can change the... In the function this Point to
- callThe main function of inheritance is to realize inheritance
function Father(uname, age, sex) {
this.uname = uname;
this.age = age;
this.sex = sex;
}
function Son(uname, age, sex) {
Father.call(this, uname, age, sex);
}
var son = new Son(' Lau Andy ', 22, ' male ');
console.log(son);
// { uname: ' Lau Andy ', age: 22, sex: ' male ' }apply()
thisgrammar
fun.apply(thisArg, [argsArray])- thisArg: stay fun Specified by function runtime this value
- argsArray: Value delivered , Must be included in
ArrayInside
- The return value is the return value of the function , Because it's the calling function
var ldh = {
name: ' Lau Andy '
}
function f(arr) {
console.log(this); //ldh
console.log(arr); // Xiaohong
}
f.apply(ldh, [' Xiaohong ']);effect
- It's also a call function The second one can change the inside of the function this Point to
- But its parameters must be arrays ( Pseudo array )
- apply The main application of For example, we can use apply Find the maximum with the help of mathematical built-in objects Math.max()
var arr = [1, 5, 66, 44, 8];
var max = Math.max.apply(Math, arr);
console.log(max); //66
// console.log(Math.max(...arr));
// This can also , We are here to practice apply Usage of bind()
bind()grammar
fun.bind(thisArg, arg1, arg2, ...)- thisArg: stay fun Specified by function runtime this value
- args1, arg2, ... : Other parameters passed
- bind Methods do not call functions , But it can change the inside of the function this Point to
- The return value is fun Modified copy of the original function ( New functions are generated )
var ldh = {
name: ' Lau Andy '
}
function f(a, b) {
console.log(this); //ldh
console.log(a + b); //5
}
var fn = f.bind(ldh, 2, 3);
fn();effect
- Will not call the original function
- You can change the internal of a function this Point to
- Return is the original function change this And the new functions that come out of it
- If there are functions we don't need to call immediately , But I want to change the internal of this function this Point to , Use this timebind
Timer
<button> Click on </button>var btn = document.querySelector('button');
btn.onclick = function() {
this.disabled = true;
var that = this;
setTimeout(function() {
that.disabled = false;
}, 3000)
}var btn = document.querySelector('button');
btn.onclick = function() {
this.disabled = true;
setTimeout(function() {
this.disabled = false;
}.bind(this), 3000)
}summary
callapplybindThe same thing
Difference
- call and apply Can bring Oh function , And change the inside of the function this Point to
- call and apply The parameters passed are different call:arg1, arg2, ...apply:[arg] Array form
- bind Does not call function , You can change the inside of the function this Point to
Main 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
Last
边栏推荐
- C语言经典练习题(2)——“冒泡排序(Bubble Sort)“
- Worthington's test of hepatocyte separation system and related optimization schemes
- 训练赛《眼不见,心不烦,理不乱》题解
- Four characteristics of nb-iot
- 组合数(阶乘的质因子的个数,组合数的计算)
- Problem solution of supporting problem solution of heavy chain dissection example
- The local picture cannot be displayed after the uniapp H5 is packaged
- Cve-2022-29464 wso2 file upload vulnerability
- What is the product and expressiveness of 113700 Xingrui? Come and have a look
- [novice] develop a tab component that is easy to expand by hand
猜你喜欢

buu web

Redis transaction learning

adobe PR2022 没有开放式字幕怎么办?

Anchor point and anchor frame of target detection

An accident caused by MySQL misoperation, and "high availability" can't withstand it

Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
![[development technology] spingboot database and Persistence technology, JPA, mongodb, redis](/img/fb/bb4a26699e5ec10c6881a4c95ac767.png)
[development technology] spingboot database and Persistence technology, JPA, mongodb, redis

(008) flask is OK if you have a hand -- database migration flask migrate

Summary of Zhang Yu's 30 lectures on Advanced Mathematics

Worthington mammalian lactate dehydrogenase study -- Characteristics and determination scheme
随机推荐
值为 0 流程,另一部分看括但不限于如下这题是
Redis
Sqlserver backup restore
Remember an online sql deadlock accident: how to avoid deadlock?
Sword finger offer | minimum number of rotation array
Worthington's test of hepatocyte separation system and related optimization schemes
嵌入式系统移植【6】——uboot源码结构
6-15 vulnerability exploitation SMB rce remote command execution
93. (leaflet chapter) leaflet situation plotting - modification of attack direction
H7-tool serial port offline burning operation instructions, support TTL serial port, RS232 and RS485 (2022-06-30)
[cloud native] get to know kubernetes quickly
硬件知识3--IIC协议
Listen for the scroll event @scroll of div
"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 (summary of some topics)
ACM warm-up Exercise 4 in 2022 summer vacation (summary)
iPhone手机绑定163邮箱解决方案
6-13 vulnerability exploitation -smtp brute force cracking
I wrote code for openharmony, and the second phase of "code" pioneer officially opened!
Yu zhirs] below refers to the return structure push sent to the remote terminal
Istio architecture extension mechanism