The use of arrow function and this pointing

Arrow function: it is also a way to define function

How to define a function:


const day = function(){}

2. Defining functions in literal quantities of objects

const obj={

3. Arrow function in ES6

const day =() =>{
Parameter problem of arrow function

1.1 put in two parameters

const sum = (num1,num2) => {
  return num1+num2

1.2 put a parameter (brackets can be omitted)

const power = num => {
  return num*num
Return value problem

2.1 When there is only one line of code in the function code block:

const sum = (num1,num2) => num1+num2

This in arrow function
const obj = {
        console.log(this); //  The print is window
     settimeout(() => {
        console.log(this); // It prints obj objects

Conclusion: the search method of this in the arrow function is to search this layer by layer in the outer scope until there is a definition of this

Recommended Today

Lua language novice simple tutorial

1、 Foreword Lua is a lightweight and compact scripting language, which is written in standard C language and open in the form of source code. Its design purpose is to be embedded in the application, so as to provide flexible expansion and customization functions for the application. Lua can be applied in game development, independent […]