0

in finally really trying to understand lexical scoping and this, I've made this code.

in normalFunction, its scope is the inside of itself, the first this.a is aaa after hoisting?

in anonymousFunction, its scope is lexical, the scope from where the function express from created (the global / window object). if this.z wasn't declared, then the first this.z console log would be undefined because there's no hoisting here?

this.a = 'outer';
this.z = 'outer';

function normalFunction() {
    console.log('first: ', this.a); // first: aaa
    return function y() {
        this.a = 'aa';
        console.log(this.a); // aa
        return function z() {
            this.a = 'aaa';
            console.log(this.a); // aaa
        }
    }
}

const anonymousFunction = () => {
    console.log('first: ', this.z); // first: outer
    return () => {
        this.z = 'zz';
        console.log(this.z); // zz
        return () => {
            this.z = 'zzz';
            console.log(this.z); // zzz
        }
    }
}

normalFunction(1)(2)(3);
anonymousFunction(1)(2)(3);
totalnoob
  • 2,131
  • 5
  • 27
  • 54

0 Answers0