Getting function object arguments

Discussion in 'JavaScript' started by ::Mike::, Jun 21, 2010.

  1. #1
    So for example I call my "test" function like this:

    test({test1:'blah1',test2:'blah2'});

    How am i able to get all these variables that could be endless?

    The standard way i know which is:
    test('blah1','blah2');
    and with this you can use the arguments object to get them.

    But how am i able to get the names and values such as my 1st example?

    Thanks.
     
    ::Mike::, Jun 21, 2010 IP
  2. meloncholy

    meloncholy Peon

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Interesting question. I had a look and found this page (openjs.com/articles/optional_function_arguments.php) that includes an example of iterating optional arguments through associative arrays.

    
    function person(options) {
    	var default_args = {
    		'name'	:	"Binny V A",
    		'me'	:	true,
    		'site'	:	"http://www.bin-co.com/",
    		'species':	"Homo Sapien"
    	}
    	for(var index in default_args) {
    		if(typeof options[index] == "undefined") options[index] = default_args[index];
    	}
    	
    	/* options[] has all the data - user provided and optional */
    	for(var i in options) {
    		alert(i + " = " + options[i]);
    	}
    }
    person({
    		'name'	:	"George W. Bush",
    		'me'	:	false,
    		'site'	:	"miserable failure"
    	});
    
    Code (markup):
     
    meloncholy, Jun 21, 2010 IP