Day 85, closure

More interviews. I guess having interview shouldn’t be news. If there were one day without an interview, that should be news.

Closure, closure, closure

Definition:
Closure is a function that captures the external bindings (i.e. not its own arguments) contained in the scope, in which it was defined for later use (even after that scope has completed).

var outFn = function(){
  var closureVar = 'test';
  var inFn = function(arg){
    console.log('this is ' + closureVar + ' with ' + arg);
  };
  return inFn;
};

var closureFn = outFn();
closureFn(' more args');

This is super basic sample. An nice StackOverflow article to read: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work

An even better one:

http://javascriptweblog.wordpress.com/2010/10/25/understanding-javascript-closures/

About these ads
Standard

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