Passing & working with multiple arguments to function

Discussion in 'JavaScript' started by smcblogger, Dec 10, 2009.

  1. #1
    function bad_color(id,id2) {
    document.getElementById(id).bgColor = "pink";
    document.getElementById(id2).bgColor = "pink";
    }
    The above works as I want, but is there a better way to do this? I'm going to have to pass a lot more than 2 arguments to the function at a time. Pass an array and have a for statement in the function? Thinking out loud.

    Thanks for any help. :D
     
    smcblogger, Dec 10, 2009 IP
  2. smcblogger

    smcblogger Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ok, got something working. Thanks anyway guys. :)
     
    smcblogger, Dec 10, 2009 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    the best practice these days is to pass a single object instead of stacking up multiple arguments and if - then - else and copy/pasting code.

    for instance, consider using something like this function which can support ANY number of elements passed to it w/o changing the function itself:

    
    var setBackgrounds = function(options) {
        // deal with default options passed (or not passed)
        options = options || {}; // options should always be an object
        options.elements = options.elements || ["edit13110491","td_post_13109496"]; // default elements to apply to, array of ids
        options.backgroundColor = options.backgroundColor || "pink"; // default colour to apply
        
        // loop through elements
        var count = options.elements.length, el;
        while(count--) {
            el = document.getElementById(options.elements[count]);
            if (el)
                el.style.backgroundColor = options.backgroundColor;
        }
    };
    
    setBackgrounds({
        elements: ["vB_Editor_001_smiliebox"],
        backgroundColor: "blue"
    });
    
    // run with default elements and colour:
    setBackgrounds();
    
    // run with default elements and a black bg:
    setBackgrounds({
        backgroundColor: "#000"
    });
    
    Code (javascript):
    this allows you great flexibility as the options are 'private' to the function and when you need to add new arguments, you don't need to change all places where you call it, just take care of a decent default set and you're off.
     
    Last edited: Dec 11, 2009
    dimitar christoff, Dec 11, 2009 IP
  4. smcblogger

    smcblogger Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I appreciate your response. I am a nube at js, so I have no idea what your response means. I appreciate all the comments, but still no clue. :confused:
     
    smcblogger, Dec 11, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    You could do it like this:
    
    // This is creating an array of objects, in format of property: value, property: value...
    var a = [
    {"id":"element1", "color": "red"},
    {"id":"element2", "color": "blue"},
    {"id":"element3", "color": "green"}
    ];
    bad_color(a);  //sends our object to the function
    
    function bad_color(args) {
    	for(i=0; i<args.length; i++) {  //iterate through each object in the array
    		document.getElementById(args[i].id).bgColor = args[i].color;    
    	}
    }
    
    Code (markup):
     
    Last edited: Dec 12, 2009
    camjohnson95, Dec 12, 2009 IP
  6. smcblogger

    smcblogger Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok, I understand that, and it works!!! Cool. Thanks. :)
     
    smcblogger, Dec 17, 2009 IP