1

I am trying to understand closure and I understand that a function closes over the variables of its outer scope. But it seems like, it's not just outer scope but any scope outside the function. For example this code

function func (arg) {
    return function f1() {
        return function f2(){
            console.log(arg)
        }
    }
}

var t1 = (func("test1"))();
var t2 = (func("test2"))();
t1();
t2();

works and anytime you call t1 or t2, they print out test1 and test2 respectively. So my question is this, does function closure work for any scope outside the function?

Oleks G
  • 94
  • 5

0 Answers0