How to declare and work with array of user defined functions in JavaScript

by Jagadish Pulakhandam on 8/3/2012 7:25:52 AM
Rated 0 from 0 votes
Brief: Demonstrates on declaring, defining and working with function arrays in JavaScript
Posted to: Advanced JavaScript Programming/Development tutorials/videos - User Defined Functions
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached source code/video demonstrates the following:
  • How to define anonymous functions
  • How to store functions as elements in arrays (function array)
  • How to loop (parse) through all functions in an array and execute them

Source Code:

01.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03.<html>
04.<head>
05.    <title></title>
06.    <script>
07.        //DEMO: storing (anonymous) functions as elements in an array
08. 
09.        var ary = []; //defining an array
10. 
11.        //first element in an array
12.        ary[0] = function () {
13.            alert("First");
14.        };
15. 
16.        //second element in an array
17.        ary[1] = function () {
18.            alert("Second");
19.        };
20. 
21.        //third element in an array
22.        ary[2] = function () {
23.            alert("Third");
24.        };
25. 
26.        //iterate through array and execute all functions (available as elements)
27.        for (var i = 0; i < ary.length; i++) {
28.            ary[i](); //calls function available at i'th location in the array
29.        }
30. 
31.    </script>
32.</head>
33.<body>
34. 
35.</body>
36.</html>


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following