JavaScript OOP Tutorial: Anonymous Functions

This is a JavaScript DOM exercise from Chapter 3 of Microsoft AJAX Library Essentials: JavaScript in ASP.NET AJAX 1.0 Explained.

Anonymous functions can be created ad-hoc and used instead of a named function. Although this can hinder readability when the function is more complex, you can do this if you donít intend to reuse a functionís code. In the following example we pass such an anonymous function to DisplayGreeting(), instead of passing GetCurrentHour():

// call DisplayGreeting
DisplayGreeting(
  function()
  { 
    return (new Date()).getHours(); 
  }
);

This syntax is sure to look strange if this is the first time you work with anonymous functions. You can compact it on a single line if it helps understanding it better:

DisplayGreeting( function() { return (new Date()).getHours(); } );

Implement the exercise step by step and find detailed explanations in our book, Microsoft AJAX Library Essentials: JavaScript in ASP.NET AJAX 1.0 Explained.