Can someone explain it to me what closure is in real simple language ?
Possible Duplicate:
What are 'closures' in .NET?
I am currently looking at lambda expression and the word closure keeps coming. Can someone explain it to me in real simple language.
I'd say this is a duplicate of: What are 'closures' in .NET?
"In essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was first created - ie it can still use the local variables etc of the method which created it, even after that method has finished executing."
Your shoes are in the hall; your jacket is in the kitchen. Put them on, and your gloves (they're in the drawer), when going outside.
Now you can go playing with your cars. At eleven o'clock you must go buy some bread in the corner store.
Kid plays. Forgets all the world.
Alarm clock goes off; kid sees: eleven o'clock! Oh - go outside to buy bread using the "going outside" closure.
I like the Google example for Javascript (you can morph it for C# easily). It's not something a 5 year old would understand but then I doubt an average 5 year old would understand what a function was.
/*
* 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 = 'Hai!';
var content = document.getElementById('content');
function innerFunction() {
content.innerHTML = someNum + ': ' + someString;
content = null; // IE memory leak for DOM reference
}
innerFunction();
}
链接地址: http://www.djcxy.com/p/150.html
下一篇: 有人可以向我解释什么是真正简单的语言?