当前位置:网站首页>JS中的this指向
JS中的this指向
2022-04-23 05:53:00 【KK要继续努力】
JS中的this指向
this指向是由调用方式决定的
- this在全局作用域:指向window
console.log(this); //window
- this在函数作用域
- 普通函数:严格模式下this为undefined,非严格模式this为window
function fun(){
console.log(this);
}
fun(); //window
'use strict'
function fun(){
console.log(this);
}
fun(); //undefined
- 箭头函数:都为window
var fun = ()=>{
console.log(this);
}
fun(); //window
- this在对象的方法中(对象上下文规则)
- 普通函数
(1)对象.方法():this指向打点的调用对象
var obj = {
name: 'xxx',
func: function() {
console.log(this);
}
}
obj.func(); //obj
(2)数组[下标]():数组(类数组对象)枚举出函数进行调用,this是这个数组(类数组对象)
var arr = ['A','B','C', function(){
console.log(this);
console.log(this[0]);
}];
arr[3](); //['A', 'B', 'C', ƒ] A
(3)立即执行函数:this指向window对象
var a = 1;
var obj = {
a: 2,
fun: (function(){
var a = this.a;
return function(){
console.log(a+this.a);
}
})()
}
obj.fun(); //3
(4)定时器、延时器:this指向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;
//注意此种方法与上面不同,这是单独定义一个函数再调用
//这种是对象.方法()
setTimeout(function(){
obj.fun()}, 1000); //3
(5)事件处理函数的上下文是绑定事件的DOM元素
元素.click = function(){
console.log(this); //元素
}
- 箭头函数
所有情况都为window
特殊情况
var obj = {
name: 'hh',
func: function() {
//定时器里为普通函数是this指向window
setInterval(function(){
console.log(this);
},1000)
}
}
obj.func(); //window
var obj1 = {
name: 'hh1',
func: function(){
//为箭头函数是this指向obj
setInterval(()=>{
console.log(this);
},1000)
}
}
obj1.func(); //obj1
版权声明
本文为[KK要继续努力]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45393020/article/details/123767952
边栏推荐
- QT icon application
- Detailed explanation and application principle of token
- JS高频面试题
- Camera calibration: key point method vs direct method
- 客户端软件增量更新
- 基于Keras的时装分类案例
- Qt 添加QSerialPort类 实现串口操作
- Vs can be compiled, but there will be a red underline to indicate the problem of undefined identifiers
- [UDS unified diagnostic service] III. application layer protocol (1)
- C语言进阶要点笔记2
猜你喜欢
Collection of practical tips for C language (continuously updated)
CUDA环境安装
js获取链接?后边的参数名称或者值,根据url ?后的参数做判断
基于VGG卷积神经网络的图像识别代码实现
QT icon application
VHDL arbitrary frequency divider (50% duty cycle)
Initialization of classes and objects (constructors and destructors)
Makefile基础、常用函数及通用Makefile
[UDS unified diagnosis service] i. diagnosis overview (1) - diagnosis overview
Qt 给应用程序加图标
随机推荐
uniapp 自定义搜索框适配小程序对齐胶囊
CUDA环境安装
2020 Jiangsu Collegiate Programming Contest-A.Array
TP download folder, compress folder and download
Qt 给应用程序加图标
类和对象
基于TensorFlow的线性回归实例
约瑟夫序列 线段树 O(nlogn)
VHDL arbitrary frequency divider (50% duty cycle)
Assembly base code example
软件工程中的十三种文档
Initialization of classes and objects (constructors and destructors)
[UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)
Introduction to nonparametric camera distortion model
C语言结构体指定初始化
在visual stdio中运行qt程序
【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)
微信小程序之改变数组中某值,对象中某值的方法
三极管原理及特性分析
Matching between class template with default template argument and template parameter