国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

javascript - Understanding closures? Seek clarification.
高洛峰
高洛峰 2017-05-18 10:49:50
0
4
629
function f1(){//2、找到 f1 函數(shù),執(zhí)行。
    var n=999;//3、給變量 n 賦值。
    nAdd=function(){n+=1}//9、找到 nAdd ,匿名函數(shù)內(nèi)沒有變量 n ,需要去上層查找,n = 999 +1。
    function f2(){//5、找到 f2 函數(shù),執(zhí)行。
        alert(n);//6、執(zhí)行動作。
    }
    console.log(n);//新加上,測試,不參與執(zhí)行步驟。
    return f2;//4、返回 f2 函數(shù),需要尋找 f2 函數(shù)。
}
var result=f1();//1、將 f1函數(shù)的返回值賦值給 result 變量,result 也變成函數(shù),需要尋找f1函數(shù)。
result(); //7、第一次執(zhí)行 result 函數(shù),將步驟 6 的執(zhí)行動作(步驟 6)結(jié)果輸出,n 等于 999。
nAdd();//8、執(zhí)行 f1 函數(shù)里的全局變量函數(shù) nAdd ,需要尋找 nAdd 函數(shù)。
result(); //10、第二次執(zhí)行 result 函數(shù),將步驟 5 的執(zhí)行動作(步驟 6)結(jié)果輸出,此時 n 等于 1000,因為第一次執(zhí)行 result 函數(shù)時,查找了上層的作用域,n 是 999。
nAdd();//11、如果再執(zhí)行 nAdd 函數(shù),此時 nAdd 這個函數(shù)里的 n 是 1000,而 f1 函數(shù)的 n 還是 999,也就是說 f1 的變量 n 和 nAdd 的 n 是兩個作用域不同的同名變量。
result(); 
f1();//新加上,測試

/*結(jié)果
控制臺輸出:999
彈窗:999
彈窗:1000
彈窗:1001
控制臺輸出:999
*/

I would like to ask the seniors to see if this understanding is correct.
Supplement: Can it be understood that when the closure is executed for the first time, it needs to search for variables in the upper layer. After finding it, the variable value of the upper layer becomes the variable value of the sub-function, and there is no need to go to the upper layer in the future. The search, because it has been inherited during the first execution, becomes its own.
It feels a bit messy. . .
(face covering

--------------------Added again--------------------
The more I read The more chaotic it becomes.
Then it was a complete mess.
Judging from the output results, the n of f1 is immutable in the first console output and the last console output.
But can’t sub-functions read variables from each other? Why does the expression of nAdd affect n of f2?

高洛峰
高洛峰

擁有18年軟件開發(fā)和IT教學經(jīng)驗。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項目經(jīng)理、高級軟件工程師等職務。 網(wǎng)絡人氣名人講師,...

reply all(4)
某草草
function f1(){
    var n=999;
    nAdd=function(){n+=1}
    function f2(){
        alert(n);
    }
    console.log(n);
    return f2;
}
var result=f1();
result(); 
nAdd();
result();
nAdd();
result(); 
var b = f1();//新加上,測試

At this time, b and n in result are not the same.
b and n in nAdd are the same.
n in result cannot be changed.

洪濤

I have the same question, so I copied my answer and added some more.

  1. var result=f1(): The f1 function returns the f2 function
    Assign the returned f2 function to the result global variable, (the scope chain of f2 is saved to the result global variable)

  2. result(): Call result(), which forms a closure: has the right to access variables in another function scope
    Because the scope in f2 refers to the local variable n in f1, when f1 is executed Finally, the garbage collection mechanism finds that the n variable is still referenced in result, so the garbage collection mechanism will not release n.
    So that n is always saved in the result scope chain. The scope chain of result can normally access the local variable n in f1, forming a closure.

  3. nAdd(): nAdd does not write var, so nAdd is a global variable. When calling nAdd() and result(), a closure will be formed. The anonymous function function(){n+=1} has the local n in the scope chain. variable, so when nAdd=funtion(){n+=1}, the scope chain of this anonymous function is saved to the global variable nAdd to form a closure, and nAdd() is called to find the f1 local variable n=999, n+ in the scope chain 1=1000.

  4. result(): result() outputs 1000

  5. nAdd(); Repeat the third step n+1 = 1001

  6. result(); Repeat the fourth step and output n

  7. f1(); No closure is formed when f1 is called, n is always 999, because n is destroyed by the garbage collection mechanism after each execution of f1, so var n=999 is called again here; the subsequent output is also It’s 999

Why does the expression of nAdd affect n of f2?
Because of the closure, n has never been destroyed. nAdd() also formed a closure and changed the value of n, so result() is called again later. n has not been destroyed and the recycling has been +1, so it will be Influence.
Finally, there is no closure when f1() is called, and n was destroyed before. So it always outputs a=999;

This is just my understanding, if there are any mistakes please let me know

黃舟

I suggest you read this
http://www.ruanyifeng.com/blo...
It’s explained more clearly

曾經(jīng)蠟筆沒有小新

It’s too complicated to say

This closure uses the static scope feature of js to achieve this effect

function f1(){
    var n=999;// 這里標識n 為代號 n1
    nAdd=function(){n+=1}+1。
    function f2(){
        alert(n);
    }
    console.log(n);
    return f2;
}
var result=f1();
result(); 
nAdd();
result(); 
nAdd();
result(); 
f1();

The first call to result():

alert(n); is looking for n1

nAdd(); also adds the value of n1

The same goes for the ones behind

And the last f1();

When running, var n = 999; assigns a value to n1.
The n of console.log(n) is also n1, so the printed value is 999

Your example is a bit simple. You can look at a complex example (focus on the description of static scope):
static scope

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template