日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

我也谈javascript闭包

發(fā)布時間:2024/1/17 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 我也谈javascript闭包 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、什么是閉包呢?
Whenever you see the function keyword within another function, the inner function has access to variables in the outer function

function foo(x) { var tmp = 3; return function (y) { alert(x + y + (++tmp)); // will also alert 16 } } var bar = foo(2); // bar is now a closure. bar(10);

/* * When a function is defined in another function and it * has access to the outer function's context even after * the outer function returns. * * An important concept to learn in JavaScript. */function outerFunction(someNum) { var someString = 'Hey!'; var content = document.getElementById('content'); function innerFunction() { content.innerHTML = someNum + ': ' + someString; content = null; // Internet Explorer memory leak for DOM reference } innerFunction(); } outerFunction(1);?


Two one sentence summaries:

  • a closure is the local variables for a function — kept alive?after?the function has returned, or
  • a closure is a stack-frame which is?not deallocated?when the function returns (as if a 'stack-frame' were malloc'ed instead of being on the stack!).

The following code returns a reference to a function:

function sayHello2(name) { var text = 'Hello ' + name; // Local variable var sayAlert = function() { alert(text); } return sayAlert; } say2 = sayHello2('Bob'); say2();

Most JavaScript programmers will understand how a reference to a function is returned to a variable in the above code. If you don't, then you need to before you can learn closures. A C programmer would think of the function as returning a pointer to a function, and that the variables?sayAlert?and?say2were each a pointer to a function.

There is a critical difference between a C pointer to a function and a JavaScript reference to a function. In JavaScript, you can think of a function reference variable as having both a pointer to a function?as well?as a hidden pointer to a closure.

The above code has a closure because the anonymous function?function() { alert(text); }?is declared?inside?another function,?sayHello2()?in this example. In JavaScript, if you use the?functionkeyword inside another function, you are creating a closure.

In C, and most other common languages?after?a function returns, all the local variables are no longer accessible because the stack-frame is destroyed.

In JavaScript, if you declare a function within another function, then the local variables can remain accessible after returning from the function you called. This is demonstrated above, because we call the function?say2()?after we have returned from?sayHello2(). Notice that the code that we call references the variable?text, which was a?local variable?of the function?sayHello2().

function() { alert(text); } // Output of say2.toString();

Click the button above to get JavaScript to print out the code for the anonymous function. You can see that the code refers to the variable text. The anonymous function can reference text which holds the value?'Bob'?because the local variables of?sayHello2()?are kept in a closure.

The magic is that in JavaScript a function reference also has a secret reference to the closure it was created in — similar to how delegates are a method pointer plus a secret reference to an object.

? ?

總結(jié)

以上是生活随笔為你收集整理的我也谈javascript闭包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 污污网站在线观看视频 | 亚洲国内自拍 | 日本美女久久 | 色爱视频 | 97超碰免费在线 | 琪琪色视频 | 影音先锋毛片 | 黄色成人在线网站 | 美国av导航 | 国产大片91| 国产精品久久久久久亚洲影视 | 91成人网页| 性欧美ⅹxxxx极品护士 | 日韩精品中文字幕一区二区三区 | 久操福利 | 亚洲午夜精品在线观看 | 欧美xxxxx少妇| 成人在线免费网站 | 成人激情小视频 | 少妇又紧又深又湿又爽视频 | 久青草国产在线 | 青青青在线观看视频 | 热久久中文字幕 | 国产精品1区2区3区4区 | 久久精品小视频 | 成人久久视频 | 久久狠狠爱 | 亚洲色图校园春色 | 久久免费视频网站 | 中日韩欧美在线观看 | 先锋资源在线视频 | www三级免费 | 给我免费观看片在线电影的 | 大桥未久中文字幕 | 色噜噜成人 | 日韩精彩视频在线观看 | 伊人婷婷在线 | 香蕉久草| 天堂在线中文资源 | 69色视频| 亚洲国产日本 | 亚洲av无码一区二区乱孑伦as | 欧美一区二区免费电影 | 亚洲欧洲在线播放 | av免费观看入口 | 日韩av在线第一页 | 狼干综合 | 亚洲s码欧洲m码国产av | 激情综合久久 | 色婷婷综合久久久久中文字幕 | 激情五月婷婷综合网 | 99热在线免费观看 | 国产在线喷水 | www狠狠操| 亚洲成人网在线观看 | 亚洲精品女人久久久 | 日日夜夜一区 | 波多野结衣黄色片 | 免费福利在线视频 | 国产视频最新 | 日本三级韩国三级美三级91 | 欧美一区二区网站 | 天天躁夜夜操 | 777米奇影视第四色 五月丁香久久婷婷 | 欧美少妇诱惑 | av观看网| 久久久性 | 久久精品—区二区三区舞蹈 | 亚洲在线综合 | 亚洲AV无码成人国产精品色 | 中国少妇色 | 国产伦精品一区三区精东 | 2018狠狠干 | 国产成年人 | 国产精品久久国产精麻豆96堂 | 用舌头去添高潮无码视频 | 精品99在线观看 | 日韩亚洲欧美一区二区三区 | 丝袜一级片 | 中文字幕免费在线看线人 | 日韩成人精品一区二区 | 夜夜骑天天操 | 日韩啊啊啊 | 免费无遮挡无码永久在线观看视频 | 成年人免费看的视频 | 国产在线中文字幕 | 成人深夜小视频 | 免费精品一区 | 精品国产伦一区二区三 | 日韩中字在线 | 香蕉网在线观看 | 欧美日韩一区二区三区国产精品成人 | 免费在线看黄色片 | 免费色网| 亚洲av无码国产综合专区 | 精东传媒在线观看 | 国产视频一区二区三 | 在线免费观看小视频 | av第一福利大全导航 |