当前位置:网站首页>this inside the object points to the problem
this inside the object points to the problem
2022-08-07 10:12:00 【znhyXYG】
1.
const obj2 = {fn: () => {console.log(this);} }}obj2.fn(); // window
The arrow function in the above code does not have its own this, so it looks up the this object (there is no scope in the object braces?), so it finally points to the window.
2.
const obj2 = {fn: function() {console.log(this); // obj2} }};obj2.fn();
The above code fn function is called by the obj2 object, this points to obj2
Second, the function in the object returns a function
1.
const obj2 = {fn: function() {console.log("1", this);return function() {console.log("2", this);} }} }}obj2.fn()(); // 1 obj2 2 window
The above code fn returns a function. This function is not obviously called by anyone, that is, it is called by window.
2.
const obj2 = {fn: function() {console.log("1", this);return () => {console.log("2", this);} }} }}obj2.fn()(); // 1 obj2 2 obj2
The above code fn returns an arrow function. Although there is no clear caller, the arrow function may involve searching for this in the upper scope. The upper scope is the scope of the fn function, becauseCalled by obj2, so this points to obj2.
Three, there is an immediate execution function in the function
1.
const obj2 = {fn: function() {console.log("1", this);(function() {console.log("2", this);} })();return () => {console.log("3", this);} }} }}obj2.fn()(); // 1 obj2 2 window 3 obj2Here, an immediate execution function is executed, there is no explicit caller, this points to window.
2.
const obj2 = {fn: function() {console.log("1", this);(() => {console.log("2", this);} })();return () => {console.log("3", this);} }} }}obj2.fn()(); // 1 obj2 2 obj2 3 obj2
This is basically the same as the above example, except that the immediate execution function is an arrow function. The arrow function looks for this in the upper scope, so it finds this in fn,
Fourth, the situation with timer
1.
const obj2 = {fn: function() {console.log("1", this);(() => {console.log("2", this);})();return () => {console.log("3", this);setTimeout(() => {console.log("4", this);} }, 0);} }} }}obj2.fn()(); // 1 obj2 2 obj2 3 obj2 4 obj2Is the timer callback function here an arrow function, or it involves searching for this in the upper-level scope.
2.
const obj2 = {fn: function() {console.log("1", this);(() => {console.log("2", this);})();return () => {console.log("3", this);setTimeout(function() {console.log("4", this);} }, 0);} }} }}obj2.fn()(); // 1 obj2 2 obj2 3 obj2 4 windowThe timer callback function here does not have a clear caller, and this points to window.
Additional about arguments in arrow functions
In arrow functions, similar to this, arguments also point to arguments in the previous function.
function f1(x, y) {return (z) => {console.log(arguments);} }}const tempFn = f1(1, 2);tempFn(3);/*[Arguments] { '0': 1, '1': 2 }边栏推荐
- Material Design
- Problems encountered by the custom push ringtone plugin
- 这种调试方法,你值得拥有
- New in ABP 6.0.0-rc.1
- 非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
- 360企业安全云亮相2022全球数字经济大会 夯实中小微企业数字化底座
- 世界上最大的开源基金会 Apache 是如何运作的?
- Redis只能做缓存?太out了!
- Get all table names and table names with time strings in Mysql Use BetweenAnd to filter the interval range
- 技术总监需要具备哪些能力?优先级?
猜你喜欢

MapStruct 高级用法

2022 Niuxue Multi-School League Game 6 Solution

Flask Framework - Application Error Handling

2022牛客多校联赛第六场 题解

php怎么判断目录下有几个文件

颜值爆表!Redis官方可视化工具来啦,功能真心强大!
![[C language] Tower of Hanoi](/img/4a/331a88cf1d21b3aed537883940d3d6.png)
[C language] Tower of Hanoi

The non-professional AI brother is on fire: he doesn't have an ML degree, but he got an offer from DeepMind

NProgress plugin (progress bar)

技术总监需要具备哪些能力?优先级?
随机推荐
This debugging method, you deserve it
Mastering Andriod Reverse in 100 Days - Day 1: ADB Principles and Common Commands
ES6之原型
YOLOV5 Study Notes (6) - Optimizing Network Architecture
Redis是单线程的,但Redis为什么这么快?
非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
formData
Project Optimization DrawCall Optimization (Unity3D)
命令执行漏洞
Flask框架——应用错误处理
Some summary about common build tools
UGUI series-InputField limits the number of inputs and limits the input format
Project Optimization Data Collection Optimization (Unity3D)
Learn to Arthas, 3 years experience with 5 years work force you!
发现一款好用到爆的数据库工具,被惊艳到了
Problems encountered by the custom push ringtone plugin
100天精通Andriod逆向——第1天:ADB原理及其常用命令
The snowflakes algorithm of a database table
thinkphp 6.x 任意文件写入漏洞
The non-professional AI brother is on fire: he doesn't have an ML degree, but he got an offer from DeepMind