0

If I have a function with parameters (or argument) like this:

check('red', 'blue', 'arial')

What i would like to know is can you have text like this:

check(background:'red', color:'blue', font:'arial')

In the function I have a if statement so if the parameter or argument has background: before it, it changes the background to the parameter after the background:

    function check(one, two, three){
        if (one==background:one){
           document.body.style.background= one ;
            }
        }  

I know this doesn't work, how would you do this or something like it?

Can I use a if statement but code it to detect if a parameter has 'background:' before it? Is this possible or is there a better way of doing it? I would like to use pure JavaScript if that is possible.

Jacob G
  • 13,762
  • 3
  • 47
  • 67

1 Answers1

3

JavaScript does not support labeled function arguments (a la C# and other languages). However, it would be easy enough to pass in a configuration object instead:

function check(config) {
    // config.background
    // config.color
    // config.font
}

check({ background: 'red', color: 'blue', font: 'arial' });

If you need or want the function to also support being called with regular parameters, you can always detect argument types:

function check(background, color, font) {
    if(typeof background === 'object') {
        color = background.color;
        font = background.font;
        background = background.background;
    }
    // background, color, and font are what you expect
}

// you can call it either way:
check('red', 'blue', 'arial');
check({ background: 'red', color: 'blue', font: 'arial' });

And, finally, if you don't want to (or somehow can't) modify the original function, you can wrap it:

var originalCheck = check;
check = function(config) {
    originalCheck(config.background, config.color, config.font);
}
Ethan Brown
  • 26,892
  • 4
  • 80
  • 92
  • Thank you! how would I use a configuration object like I asked in my question? Sorry if this is a elementary question. :) – Jacob G Jul 23 '14 at 15:36
  • All right,[i got it to work](http://jsfiddle.net/ImagineStudios/c5MNh/4/). Can i code it so if the value of background is red then the value of background:'red', which changes the background, becomes like a parameter and can change the background depending on the color in the config? Thank you! – Jacob G Jul 23 '14 at 15:52
  • Hm...not really sure what your'e asking there, and it sounds like a different problem. Why don't you try figuring it out, and if you can't get it, ask another SO question? – Ethan Brown Jul 23 '14 at 16:22
  • Example of what I mean: if you call a function : change('red') and the function is this: change(one){document.body.color=one;}. When you call the function you can put any color as a parameter and it will change the color of the body. Is there any way of doing this with your solution? So if i call the function: check({background:'red'}) can the value of background act as a variable? Thanks! – Jacob G Jul 23 '14 at 16:31
  • Yes. The value of `background` in the config object can be anything, including a variable. – Ethan Brown Jul 23 '14 at 18:34