0
var obj = {
    outer: function() {
        console.log(this);
        }
    }

Above code outputs Object() inside the console, as the this keyword points to the object 'obj'. Which means the 'this' variable points to the object in which it is present.

However,

function outer() {
    function inner() {
        console.log(this);
    }
}

Above code outputs window in the console. Why does it not show the function outer() in the console, as according to my knowledge, a function is also an object, which can have its own properties. So if i did this.name = "myName"; inside the inner function, i should be able to output - console.log(outer.name), but that is not the case. Is there any misunderstanding on my side or does JS work like that only ?

Dhruv Chadha
  • 919
  • 7
  • 25
  • 1
    `inner` function is not a property of `outer` function it is just in its scope – Maxx Aug 03 '16 at 14:12
  • 1
    Because you're calling it like `inner()` and not `outer.inner()`. If you assigned `outer.inner = function(){...}` and called `outer.inner()`, then `this` would point to `outer`. – Siguza Aug 03 '16 at 14:12

0 Answers0