Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'this' key word inside arrow functions [duplicate]

Tags:

javascript

trying to learn the 'this' key word inside an arrow function properly.

after reading and watching some videos i understood that with the regular function the 'this' key word will be defined whenever the function is invoked.

and 'this' inside the arrow function will be defined according to the value of 'this' wherever you build that function.

so i opened the console and played with two objects and the two functions.

i did this:

const reg = {
    reg1: "reg1",
    reg2: {
            reg3: 'reg3',
            regFunc: function(){console.log(this)}
    }
}

const arrow = {
    arrow1: "arrow1",
    arrow2: {
            arrow3: 'arrow3',
            arrowFunc: () => {console.log(this)}
    }
}

reg.reg2.regFunc()
VM712:5 {reg3: "reg3", regFunc: ƒ}

arrow.arrow2.arrowFunc()
VM712:13 Window {parent: Window, opener: null, top: Window, length: 1, frames: Window, …}

for the reg Object i get it because it was invoked inside the reg2 object which it shows me.

but for the arrow Object the this keyword in the arrow function was created inside the arrow2 object inside arrow.

so why isn't the value of it in the arrow object arrow2? why is it the global window?

like image 832
ohn Avatar asked Jan 18 '20 12:01

ohn


People also ask

Does this keyword work in arrow function?

In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever. With arrow functions the this keyword always represents the object that defined the arrow function.

Why this is not used in arrow function?

'this' in the arrow function represents from wherever it is called. for eg if i open the browser and goto console and type above code then 'this' will become window object since the function is called from global enviroment. Also arrow function doesnot have its own 'this'.

What keywords are avoided in arrow functions?

By using arrow functions, we avoid having to type the function keyword, return keyword (it's implicit in arrow functions), and curly brackets.

How this keyword in arrow gets resolved?

Arrow functions treat this keyword differently. They don't define their own context since it doesn't have its own this context. They inherit that from the parent scope whenever you call this . this in regular function always refers to the context of the function being called.


2 Answers

this is defined for a function execution context.

In the code that defines const arrow there is no function context, only the arrow function. So the lexical this is the global object (in sloppy mode) or undefined in strict mode.

It is a common misunderstanding that an object literal (the one assigned to arrow or to arrow2) would somehow bind this to it.

like image 173
trincot Avatar answered Oct 10 '22 03:10

trincot


In arrow functions this is treated similarly to a normal variable (a constant one). An arrow function records the value of this at the moment of creation and this will always point to that value inside the arrow function (since you can't reassign this).

like image 1
marzelin Avatar answered Oct 10 '22 01:10

marzelin