Halhelms
SIGN UP FOR MY NEWSLETTER

www.savorgold.com is top on wow gold and runescape gold and World of Warcraft gold provider list for trusted services. Their reputation seems to growing by the minute, which isn't surprising because they are one of the safest sellers of Gold. Delivery speed and customer service are very good. They aslo are giving some bonus items depending on the amount of gold you purchase.

 
 
Halhelms

Recent Comments

Recent Entries

RSS

JavaScript Basics: Arguments and Overloading?

Arguments in JavaScript functions are optional. I might declare that a function accepts 3 arguments -- but users of the function may pass in 0, 1, 2, or 3 arguments. Question: how do I know whether all arguments were passed in? And if only some of them were, how do I know which ones were?

First question: how do I know whether all arguments were passed into a function?

JavaScript makes this easy by giving you two variables to check: arguments.length -- the number of arguments actually passed in; and function_name.length -- the number of arguments the function was expecting.

So, if your function requires that every argument be passed in, you can do something like this:

var myFunction = function( a, b, c ) {
  if ( myFunction.length !== arguments.length ){
    return;
  };
};


Second question: if I am allowing flexibility in arguments passed in, how do I know which ones were actually provided?

This one has a less satisfactory answer, as it's only a partial answer. If you only need to check the number of arguments passed in and not necessarily the type of those arguments, John Resig has posted a great solution, found at http://ejohn.org/blog/javascript-method-overloading/.

Sometimes, though, the type of those arguments is also important.

Look at this code:

var myFunction = function( name, age, subscribe ){

};

Here, I need the person's name -- but perhaps age and whether they want to subscribe to my fabulous "Investment Secrets of the Ages" are optional. I know the types expected for these arguments:

  • name : string
  • age : number
  • subscribe : boolean

In that case, this code will help:

var myFunc = function(name , age, subscribe ){
	var args = arguments;
	var expanded_args = "";
	
	for ( var i = 0; i < myFunc.length; i++ ) {
		expanded_args += typeof args[ i ] + '_';
	};
	
	expanded_args = expanded_args.substring( 0, expanded_args.length - 1 );
	console.log( expanded_args );
	
	// overloaded functions
	var overloaded_functions = function() {
		
		return {
		
			name_age_subscribe : function() {
				// name, age, and subscribe were passed in
				var _name = args[ 0 ];
				var _age = args[ 1 ];
				var _subscribe = args[ 2 ];
				
				console.log( "Now, I'll do something with these arguments: \n" +
				"name : " + _name + "\n" +
				"age : " + _age + "\n" +
				"subscribe : " + _subscribe );
			}, 

name_age : function() { // name and age were passed in var _name = args[ 0 ]; var _age = args[ 1 ]; console.log( "Now, I'll do something with these arguments: \n" + "name : " + _name + "\n" + "age : " + _age ); },

name_subscribe : function() { // name and subscribe were passed in var _name = args[ 0 ]; var _subscribe = args[ 1 ]; console.log( "Now, I'll do something with these arguments: \n" + "name : " + _name + "\n" + "subscribe : " + _subscribe ); },

name : function() { var _name = args[ 0 ]; console.log( "Now, I'll do something with these arguments: \n" + "name : " + _name ); } } // end return } (); // end overloaded_functions

switch ( expanded_args ){ case 'string_number_boolean' : // all arguments were passed in overloaded_functions.name_age_subscribe(); break; case 'string_number_undefined' : // name and age only were passed in overloaded_functions.name_age(); break; case 'string_boolean_undefined' : // name and subscribe only were passed in overloaded_functions.name_subscribe(); break; case 'string_undefined_undefined' : // name only was passed in overloaded_functions.name(); break; default : return; };

}; // exercise the code

myFunc( 'Ann', 21, false ); myFunc( 'Bob', 31 ); myFunc( 'Carol', true ); myFunc( 'Darrell' );

For most things, frankly, this is overkill. But recently, I ran into a situation where I did need to deal with different numbers and types of functions. This is one way to do it.

This code can be downloaded from http://github.com/halhelms/Overloaded-JavaScript-Functions

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Drew's Gravatar I think this is a cool idea.

I still get the feeling that this is another way of limiting JavaScript to the confines of typed languages. I felt dread when I saw the ES4 standard had typed variables, at least integers couldn't be cast to strings. While I probably do more good than bad with typeless variables, I still feel like it opens up really creative options in JS.
# Posted By Drew | 10/19/10 2:09 AM
 
   
Clicky Web Analytics