0

Practicing js on how functions work and how context changes for this keyword when used in different places.

Example below prints a object.

var a = {
        name:"test",
        fn:function(){
           console.log(this); 
        }
a.fn();

But when I run following code this is not

var a = {
        name:"test",
        fn:function(){ 
            var fn1 = function(){
               console.log(this);
            }
           fn1 ();
        }
a.fn();

it prints Global execution context why? Shouldn't it be either printing fn context or a's context?

Vinay Prajapati
  • 6,025
  • 4
  • 36
  • 75

0 Answers0