日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。