Home / Advanced JavaScript Tutorial / Creating Robusts Functions

 

 

CodingForums
Having trouble with scripting? Visit our help forum to get the answers you need.

Jump to...
-Free JavaScripts
-JS tutorials
-JS Reference
-DHTML/CSS

-Free applets
-Web Tutorials
-Link to Us!

Free Hosting Search Tool
Search over 5,000 web hosts at the web's LARGEST hosting directory! Also, free domain registration, website design, content, and more. Visit HostIndex.com today!

- CSS Drive
- PHP Resources
- Hotels & Vacations
- Bad credit cards
- JavaScript Menus
 

Red_CurlyC035.gif (285 bytes) Examples

Lets now provide some examples using "robust" functions.

Example 1-

The first is a simple summation notation example, where a function adds up all of the numbers that get passed into it:

function summation(){
for (i=0;i<summation.arguments.length;i++){
total+=summation.arguments[i]
}

//call the function
summation(3,5,3,5,3,2,6)

Example 2- Robust Preload Image Script

By default, whenever we wish to preload images (for use with rollover effects, for example), we have to go through the tedious steps of first creating a new image object for each image, then, for each image object, specify the path to the image itself. Using the rebust function concept, we can create a function whereby the webmaster simply has to enter the names of the images he/she wishes to preload, and they automatically get preloaded:

var myimages=new Array()
function preloadimages()
{
for (i=0;i<preloadimages.arguments.length;i++){
myimages[i]=new Image()
myimages[i].src=preloadimages.arguments[i]
}
}

A simple demonstration of the above in action would be:

<head>
<script>
preloadimages("firstimage.gif","secondimage.gif",
"thirdimage.gif")
</script>
</head>

-Tutorial introduction
-Quick overview of a "normal" function
-The limitations of a normal function
-The arguments array- the secret to robustness
-Creating robust functions
-Examples

.
End of tutorial

http://javascriptkit.com
Copyright © 1997-2003 JavaScript Kit. NO PART may be reproduced without author's permission.