Tuesday, February 21, 2012

Closures

My previous understand of Closures is bad.

Derek Greer's is much better.

Some of his code:

var createGreeting = function(greeting) {

   return function (name) {

      document.write(greeting + ', ' + name + '.');

   };

};

   

helloGreeting = createGreeting("Hello");

howdyGreeting = createGreeting("Howdy");


helloGreeting("John");

helloGreeting("Sally");

howdyGreeting("John");

howdyGreeting("Sally");


// Hello, John.

// Hello, Sally.

// Howdy, John.

// Howdy, Sally

No comments:

Post a Comment