0

I have an object that looks like this:

StandardFormat({
    HeaderFont: 'greentext2',
    HeaderLinkFont: 'bluelink3',
    Backcolor: 'Black',
        ...
});

So far, I have a function that has this form:

FormatGrid(ID, HeaderFont, HeaderLinkFont, BackColor,...){}

All the parameters are listed and must be supplied in the call. What I'd like to do is replace it with this:

FormatGrid(ID, Format){}

That way, I could write something like this:

FormatGrid('TopGrid', StandardFormat); and be able to send the id of the grid and any format object.

I'm kinda stuck. How do you merge the parameters?

Thanks for your suggestions.

frenchie
  • 51,731
  • 109
  • 304
  • 510

1 Answers1

2

You could do...

function FormatGrid(ID, Format) {
    var options;
    if (typeof Format != 'string') {
       options = Format;
    } else {
       options = {
          HeaderFont: arguments[1],
          HeaderLinkFont: arguments[2],
          Backcolor: arguments[3]
       }
    }

    // Here you could then access `options.HeaderFont`.
}

jsFiddle.

This unpacks to window however.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Is the if...else necessary? I just left options = Format; but it's not working when I write alert(options.HeaderFont); – frenchie Mar 27 '11 at 23:29
  • @frenchie Do you want to allow both ways, the object literal *and* the parameters listed out? – alex Mar 27 '11 at 23:32
  • Not sure what the difference is. Inside the function, I just want to write $(this).css('background-color', BackColor); and use the parameters that I'm putting into the object StandardFormat. – frenchie Mar 27 '11 at 23:37
  • @frenchie In that case, you would need to unpack them into the function's scope. – alex Mar 27 '11 at 23:41
  • How do I do that? That's what I'm looking to do. – frenchie Mar 28 '11 at 00:31
  • I'm just looking for a simple 1-2 liner way of doing it. I have my function like this: function FormatGrid(ID, Format) {}. What do I need to write so that I can access either option.HeaderFont or HeaderFont. I don't understand the example in Fiddle. – frenchie Mar 28 '11 at 00:48
  • For now, when I look at the Inspector in Chrome, I'm getting 2 errors: StandardFormat is not defined and HeaderFont is not defined (I'm just putting the first property to see what happens). – frenchie Mar 28 '11 at 00:50
  • @frenchie Not everything is solvable with 1-2 lines. You should limit the access to the varable to only one way, either `options.HeaderFont` or `HeaderFont`. [I've asked a question myself](http://stackoverflow.com/questions/5453636/how-can-i-unpack-an-object-into-a-functions-scope). – alex Mar 28 '11 at 00:50
  • Do I need to use the eval statement to convert StandardFormat to an object? – frenchie Mar 28 '11 at 00:53
  • ok, so I got rid of one error by writing "var StandardFormat = ({...}); – frenchie Mar 28 '11 at 01:00
  • I got the 1 liner answer! See below. – frenchie Mar 28 '11 at 01:02
  • @frenchie The best practice is to access via `options.HeaderFont`. – alex Mar 28 '11 at 01:20