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