假设我们有一个containerFunction,其中定义了两个函数:
var innerFunction1link; var innerFunction2link; function containerFunction() { var someInnerVariable = 1; innerFunction1link = innerFunction1; innerFunction2link = innerFunction2; function innerFunction1() { console.log("I'm 1 " + someOuterVariable + " " + (someInnerVariable++)); } function innerFunction2() { console.log("I'm 2 " + someOuterVariable + " " + (someInnerVariable++)); } } containerFunction(); var someOuterVariable = 42; // Point A innerFunction1link(); innerFunction2link(); someOuterVariable = "WAT?!"; // Point B innerFunction1link(); innerFunction2link();
现在的问题.根据“JavaScript忍者的秘密”一书,每个闭包都有自己的私有变量集(包括稍后定义的变量,如someOuterVariable):
所以在containerFunction外部引用的innerFunction1和innerFunction2作为innerFunction1link和innerFunction2link将具有其“私有气泡”,其中包含完整的变量集.在“A点”,两个函数都将执行正常并将输出:
I'm 1 42 1 I'm 2 42 2
然后,当我将someOuterVariable设置为“WAT?!”时,输出为:
I'm 1 WAT?! 3 I'm 2 WAT?! 4
someOuterVariable和someInnerValue的值如何在“私人气泡”中实际更新(如果有1000个气泡会怎样)?它是否实际跟踪所有变量的所有引用?如果一个闭包更新someOuterVariable,它的值将如何填充到其他闭包?
更新:
即使innerFunction将在不同的上下文中定义,它们仍将“共享”变量:
function containerFunction1() { var someInnerVariable = 1; innerFunction1link = innerFunction1; function innerFunction1() { console.log("I'm 1 " + someOuterVariable + " " + (someInnerVariable++)); } } function containerFunction2() { var someInnerVariable = 1; innerFunction2link = innerFunction2; function innerFunction2() { console.log("I'm 2 " + someOuterVariable + " " + (someInnerVariable++)); } } containerFunction1(); containerFunction2(); var someOuterVariable = 42; innerFunction1link(); innerFunction2link(); someOuterVariable = "WAT?!"; innerFunction1link(); innerFunction2link();
输出:
I'm 1 42 1 I'm 2 42 1 I'm 1 WAT?! 2 I'm 2 WAT?! 2