当前位置:网站首页>This point in JS
This point in JS
2022-04-23 17:41:00 【KK should continue to work hard】
JS Medium this Point to
this The point is determined by the calling method
- this stay Global scope : Point to window
console.log(this); //window
- this stay Function scope
- Ordinary function : In strict mode this by undefined, Nonstrict mode this by window
function fun(){
console.log(this);
}
fun(); //window
'use strict'
function fun(){
console.log(this);
}
fun(); //undefined
- Arrow function : All for window
var fun = ()=>{
console.log(this);
}
fun(); //window
- this stay Object method ( Object context rules )
- Ordinary function
(1) object . Method ():this Point to the calling object of the dot
var obj = {
name: 'xxx',
func: function() {
console.log(this);
}
}
obj.func(); //obj
(2) Array [ Subscript ](): Array ( Class array object ) List functions to call ,this This is the array ( Class array object )
var arr = ['A','B','C', function(){
console.log(this);
console.log(this[0]);
}];
arr[3](); //['A', 'B', 'C', ƒ] A
(3) Immediate execution function :this Point to window object
var a = 1;
var obj = {
a: 2,
fun: (function(){
var a = this.a;
return function(){
console.log(a+this.a);
}
})()
}
obj.fun(); //3
(4) Timer 、 Time delayer :this Point to window
setInterval(function(){
console.log(this);
}, 1000)
var obj = {
a: 1,
b: 2,
fn: function(){
console.log(this.a + this.b);
}
}
var a = 3;
var b = 4;
setTimeout(obj.fn, 1000); //7
var obj = {
a: 1,
b: 2,
fn: function(){
console.log(this.a + this.b);
}
}
var a = 3;
var b = 4;
// Note that this method is different from the above , This is to define a function separately and then call
// This is an object . Method ()
setTimeout(function(){
obj.fun()}, 1000); //3
(5) The context of the event handler is bound to the event DOM Elements
Elements .click = function(){
console.log(this); // Elements
}
- Arrow function
All cases are window
A special case
var obj = {
name: 'hh',
func: function() {
// The ordinary function in the timer is this Point to window
setInterval(function(){
console.log(this);
},1000)
}
}
obj.func(); //window
var obj1 = {
name: 'hh1',
func: function(){
// The arrow function is this Point to obj
setInterval(()=>{
console.log(this);
},1000)
}
}
obj1.func(); //obj1
版权声明
本文为[KK should continue to work hard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230551363507.html
边栏推荐
- Abnormal resolution of Xiaomi camera
- Manually implement call, apply and bind functions
- tidb-server 的配置文件在哪里?
- 122. The best time to buy and sell stocks II - one-time traversal
- QT modification UI does not take effect
- Read software engineering at Google (15)
- Hcip fifth experiment
- Open futures, open an account, cloud security or trust the software of futures companies?
- Use of shell awk command
- 394. 字符串解码-辅助栈
猜你喜欢

92. Reverse linked list II byte skipping high frequency question

Halo open source project learning (II): entity classes and data tables

JVM类加载机制
![[difference between Oracle and MySQL]](/img/90/6d030a35692fa27f1a7c63985af06f.png)
[difference between Oracle and MySQL]

Perception of linear algebra 2

2. Electron's HelloWorld
![Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers](/img/ec/43dddd18f0ce215f0f1a781e31f6a8.png)
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

Allowed latency and side output

XTask与Kotlin Coroutine的使用对比
随机推荐
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
SystemVerilog(六)-变量
Clickhouse SQL operation
How does matlab draw the curve of known formula and how does excel draw the function curve image?
Construction of functions in C language programming
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
Self use learning notes - connected and non connected access to database
2021 Great Wall Cup WP
92. Reverse linked list II byte skipping high frequency question
2021长城杯WP
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
基于51单片机红外无线通讯仿真
Matlab / Simulink simulation of double closed loop DC speed regulation system
uni-app黑马优购项目学习记录(下)
JVM class loading mechanism
1217_ Generating target files using scons
Tdan over half
2. Electron's HelloWorld
[二叉数] 二叉树的最大深度+N叉树的最大深度
Collection of common SQL statements