-1

I result I expected was Adithye. I knew other methods, but i would like to know why this method wont work.

var Person = {
    name:'Adithye',
    getName:function(){
        function xyz(){
            console.log(this.name);
        }
        xyz();
    }
}

Person.getName();
  • 1
    Why do you expect `this` inside the `xyz` function to be the `Person` object ? – Titus Jan 23 '22 at 14:56
  • Use `xyz.call(this)` in `getName` to pass the `Person` object that you called `.getName()` on as the dynamic `this` argument to `xyz`. – Bergi Jan 23 '22 at 15:19

1 Answers1

1

In the inner function, though, this no longer refers to Person. Hence, this.name is undefined in the inner function

Gary
  • 134
  • 3