Categories:

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>