Skip to content

Closures ?

January 13, 2012

What is a closure in programming world ?
Wikipedia:
“In computer science, a closure is a first-class function with free variables that are bound in the lexical environment. Such a function is said to be “closed over” its free variables. A closure is defined within the scope of its free variables, and the extent of those variables is at least as long as the lifetime of the closure itself.”

I don’t get this. Aah, is it about pass function(s) or returning function(s) in a function ?
Let me add a JS closure as an example:

function declareThis(name, age) {
var message = "Name: "+name + ", Age: " + age;
return function greet() {
console.log(message);
};
}
// Generate the closure
var declareShailesh= declareThis("Shailesh", 30);
// Use the closure
declareShailesh();

Advertisements

From → java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: