0

var sudip = {
    name: 'sudip',
    dob: 1990,
    age: function() {
        console.log(this);
        console.log(2016 - this.dob);

        function innerFunction() {
            console.log(this);
        }
        innerFunction();
    }
}
sudip.age();
connexo
  • 41,035
  • 12
  • 60
  • 87
  • When you open up a new function scope, by defining a new function with the `function` keyword you create a new `this` scope. When you call `innerFunction()` without receiver (`sudip` is the receiver in `sudip.age()`) it will default back to `window` unless you're in [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) in which case `this` will be `undefined`. If you want to use the same `this` use an [arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) instead. – 3limin4t0r Jun 15 '20 at 16:53

0 Answers0