topical media & game development
#javascript-code-05-overload.js / js
// A simple function for sending a message
function sendMessage( msg, obj ) {
// If both a message and an object are provided
if ( arguments.length == 2 )
// Send the message to the object
obj.alert( msg );
// Otherwise, assume that only a message was provided
else
// So just display the default error message
alert( msg );
}
// Both of these function calls work
sendMessage( "Hello, World!" );
sendMessage( "How are you?", window );
// A function that takes any number of arguments and makes
// an array out of them
function makeArray() {
// The temporary array
var arr = [];
// Go through each of the submitted arguments
for ( var i = 0; i < arguments.length; i++ ) {
arr.push( arguments[i] );
}
// Return the resulting array
return arr;
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.