0

first version of code :

const myobj = {
    a1 : 10,
    b1 : 20,
    c1 : 30,

    myfunc1 :function() {

    console.log(this)
    
    const myfunc2  =  function ()     {
        let a2 =100
        console.log(this)
    }
        myfunc2()

    }

}



myobj.myfunc1()

output :

{a1: 10, b1: 20, c1: 30, myfunc1: ƒ}
Window {window: Window, self: Window, document: document, name: '', location: Location, …}

second version of code :

const myobj = {
    a1 : 10,
    b1 : 20,
    c1 : 30,

    myfunc1 :function() {

    console.log(this)
    
    const myfunc2  =   () =>     {
       let a2 =100
        console.log(this)
    }
        myfunc2()

    }

}



myobj.myfunc1()

output :

Object a1: 10 b1: 20 c1: 30 myfunc1: ƒ ()[[Prototype]]: Object
Object a1: 10 b1: 20 c1: 30 myfunc1: ƒ ()[[Prototype]]: Object

I am having trouble understanding the this keyword behaviour in javascript

my problem : In my first version of code when I call this keyword inside myfunc1 then it is referencing myobj but inside the same function when I create another function as myfunc2 and call it inside the function then it is referencing the window object why it is behaving like this even though the function is called and created inside the myobj object literal

In my second version of code when I call this keyword inside myfunc1 then it is referencing myobj and inside the same function when I create another function as myfunc2 and call it inside the function myfunc1 the it is referencing the myobj object literal as the arrow function takes the scope in which it has been created

my problem is why in first version of code even though the function is created and called inside the myobj literal it is referencing the window object

firstuser
  • 9
  • 5
  • yes scoping of this is a huge pain. most of us don't even use this anymore. we used to do a hack to save a snapshot of this by doing: var that = this; – nathan hayfield Apr 15 '22 at 13:40
  • @nathanhayfield thanks for the suggestion I have tried creating a variable and assigning this as you have mentioned above it works. but I really want to know how this keyword works in javascript Iam new to javascript and this keyword in javascript is really confusing – firstuser Apr 15 '22 at 13:57
  • @firstuser take a look at the top voted answer in the question I linked. It gives a very detailed explanation of *why* – Brian Thompson Apr 15 '22 at 14:01
  • 1
    @nathanhayfield Nobody uses `var that = this` any more since ES2015 introduced arrow functions – Bergi Apr 15 '22 at 14:01
  • @bergi yes exactly why i said 'used to' – nathan hayfield Apr 15 '22 at 17:07
  • @nathanhayfield Oh I misread, I thought you meant by "*most of us don't even use this anymore.*" that people don't use the `this` keyword any more - but `that` instead. – Bergi Apr 15 '22 at 17:27
  • @bergi no i dont use either anymore. just arrow functions and actually no classes either. no worries – nathan hayfield Apr 16 '22 at 18:03

1 Answers1

0

“This” keyword refers to an object that is executing the current piece of code. It references the object that is executing the current function. If the function being referenced is a regular function, “this” references the global object. If the function that is being referenced is a method in an object, “this” references the object itself.

  • yes I know that but in the first version of my code I have called the this keyword inside the myobj object literal so according it should refer the object that is executing the current function which is myobj but it is referencing window objet – firstuser Apr 15 '22 at 14:00