alt
Advertisement
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow Microsoft AJAX arrow Microsoft AJAX Library - Functions as Variables
Site Search


Microsoft AJAX Library - Functions as Variables

Functions as Variables

In JavaScript, functions are first-class objects. This means that a function is regarded as a data type whose values can be saved in local variables, passed as parameters, and so on. For example, when defining a function, you can assign it to a variable, and then call the function through this variable. Take this example:

  1. // displays greeting
  2.  
  3. var display = function DisplayGreeting(hour)
  4.  
  5. {
  6.  
  7. if (hour >= 22 || hour <= 5)
  8.  
  9. document.write("Goodnight, world!");
  10.  
  11. else
  12.  
  13. document.write("Hello, world!");
  14.  
  15. }
  16.  
  17. // call DisplayGreeting supplying an hour as parameter
  18.  
  19. display(10);
 

When storing a piece of code as a variable, as in this example, it can make sense to create it as an anonymous function—which is, a function without a name. You do this by simply omitting to specify a function name when creating it.

  1. // displays greeting
  2.  
  3. var display = function(hour)
  4.  
  5. {
  6.  
  7. ...
  8.  
  9. }
 

Anonymous functions will come in handy in many circumstances when you need to pass an executable piece of code that you don't intend to reuse anywhere else, as parameter to a function.

Let's see how we can send functions as parameters. Instead of sending a numeric hour to DisplayGreeting(), we can send a function that in turn returns the current hour. To demonstrate this, we create a function named GetCurrentHour(), and send it as parameter to DisplayGreeting(). DisplayGreeting() needs to be modified to reflect that its new parameter is a function—it should be referenced by appending parentheses to its name. Here's how:

  1. // returns the current hour
  2.  
  3. function GetCurrentHour()
  4.  
  5. {
  6.  
  7. // obtaining the current hour
  8.  
  9. var date = new Date();
  10.  
  11. var hour = date.getHours();
  12.  
  13. // return the hour
  14.  
  15. return hour;
  16.  
  17. }
  18.  
  19. // display greeting
  20.  
  21. function DisplayGreeting(hourFunc)
  22.  
  23. {
  24.  
  25. // retrieve the hour using the function received as parameter
  26.  
  27. hour = hourFunc();
  28.  
  29. // display greeting
  30.  
  31. if (hour >= 22 || hour <= 5)
  32.  
  33. document.write("Goodnight, world!");
  34.  
  35. else
  36.  
  37. document.write("Hello, world!");
  38.  
  39. }
  40.  
  41. // call DisplayGreeting
  42.  
  43. DisplayGreeting(GetCurrentHour);
 

This code can be tested online at http://www.cristiandarie.ro/asp-ajax/Delegate.html. The output should resemble Figure 3-1.

NOTE: NET languages such as C# and VB.NET support similar functionality through the concept of delegates. A delegate is a data type that represents a reference to a function. An instance of a delegate represents a function instance, and it can be passed as a parameter to methods that need to execute that function. Delegates are the technical means used by .NET to implement event-handling. C# 2.0 added support for anonymous methods, which behave similarly to JavaScript anonymous functions.


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape