This time I will show you where this in the ES6 arrow function points to, and what are the precautions for using this in the ES6 arrow function. The following is a practical case, let's take a look.
Brief introduction: this in the arrow function points to a function defined differently from the general function. The definition of this in the arrow function: this in the arrow function is bound whendefining the function. Instead of binding when executing the function.
(1) Generally, the function this points to is bound during execution. When running obj.say(), this points to theobject of obj.
var x=11; var obj={ x:22, say:function(){ console.log(this.x) } } obj.say(); //console.log输出的是22(2) The so-called binding at definition time means that this is inherited from the parent execution context! ! This, such as this. This.x here actually represents window.x, so the output is 11.
var x=11; var obj={ x:22, say:()=>{ console.log(this.x); } } obj.say(); //输出的值为11Similar ones are: (3)
var a=11 function test1(){ this.a=22; let b=function(){ console.log(this.a); }; b(); } var x=new test1();Output 11 Arrow function situation:
var a=11; function test2(){ this.a=22; let b=()=>{console.log(this.a)} b(); } var x=new test2(); //输出22Very strange No, this is how I understand it. The specific meaning of binding this when defined in ES6 should be inherited from this in the parent execution context. It should not be the parent execution context! ! ! In this way, many unclear directions in arrow functions are solved.
Note: Simple objects (non-functions) have no execution context!
In-depth understanding of this in the arrow function
In the arrow function, the fixation of this point is not because of the internal arrow function There is a mechanism to bind this. The actual reason is that the arrow function does not have its own this at all, so the internal this is the this of the outer code block. Precisely because it does not have this, it cannot be used as aconstructor.
We can simulate the arrow function conversion in ES5:// ES6 function foo() { setTimeout(() => { console.log('id:', this.id); }, 100); } // ES5 function foo() { var _this = this; setTimeout(function () { console.log('id:', _this.id); }, 100); }So when defining an object, define the object properties. This inside usually points to the global world, or the one where the object is located. this in the environment.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
How to change the status of radio using JS
How to set bootstrap table to height percentage
The difference between var foo = function () {} and function foo()
The above is the detailed content of Where does this in ES6 arrow functions point to?. For more information, please follow other related articles on the PHP Chinese website!

同事因为this指向的问题卡住的bug,vue2的this指向问题,使用了箭头函数,导致拿不到对应的props。当我给他介绍的时候他竟然不知道,随后也刻意的看了一下前端交流群,至今最起码还有70%以上的前端程序员搞不明白,今天给大家分享一下this指向,如果啥都没学会,请给我一个大嘴巴子。

一、this关键字1.this的类型:哪个对象调用就是哪个对象的引用类型二、用法总结1.this.data;//访问属性2.this.func();//访问方法3.this();//调用本类中其他构造方法三、解释用法1.this.data这种是在成员方法中使用让我们来看看不加this会出现什么样的状况classMyDate{publicintyear;publicintmonth;publicintday;publicvoidsetDate(intyear,intmonth,intday){ye

本篇文章带大家解读vue源码,来介绍一下Vue2中为什么可以使用 this 访问各种选项中的属性,希望对大家有所帮助!

什么是this?下面本篇文章给大家介绍一下JavaScript中的this,并聊聊this在函数不同调用方式下的区别,希望对大家有所帮助!

JavaScript中箭头函数是一种比较新的语法,没有自己的this关键字,相反箭头函数的this指向包含它的作用域对象,影响方面有:1、箭头函数中的this是静态的;2、箭头函数不能作为构造函数使用;3、箭头函数不能用作方法。

jQuery是一种流行的JavaScript库,广泛用于网页开发中的DOM操作和事件处理。其中一个重要的概念就是this关键字的使用。在jQuery中,this代表当前操作的DOM元素,但在不同的上下文中,this的指向可能会有所不同。本文将通过具体的代码示例来解析jQuery中this的使用技巧。首先,让我们来看一个简单的示例:

JavaScript如何改变this指向?下面本篇文章给大家介绍一下JS改变this指向的三种方法,希望对大家有所帮助!

1.this引用1.1为什么要有this引用先来写一个日期类的例子:publicclassclassCode{publicintyear;publicintmonth;publicintday;publicvoidsetDay(inty,intm,intd){year=y;month=m;day=d;}publicvoidprintDate(){System.out.println(year+"-"+month+"-"+day);}publicstatic


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
