Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript long list of parameter; looking for alternative

Tags:

javascript

I'm writing a javascript function that has a pretty long list of parameters:

FormatGrid(ID, BackColor, SideColor, HiddenColor, SmallTextClass....) 

It works well but it's getting somewhat painful when calling this function to remember each parameter and the order they go in because the function takes a total of 9 parameters (and I might add 2 more).

I'm wondering if there's an elegant solution to this.

Thanks for your suggestions.

like image 474
frenchie Avatar asked Feb 24 '23 16:02

frenchie


2 Answers

you can just pass in an Object

FormatGrid(myObject){
//your code
}

Where your myObject is something like {ID: '1', BackColor: 'red', SideColor: 'red', HiddenColor: 'black', SmallTextClass: 'true'...}

like image 86
KJYe.Name Avatar answered Mar 16 '23 08:03

KJYe.Name


Generally I like the following format

function foo(requiredVar1,requiredVar2,..,options) {
//here is where options is merged with default params
var defaultOptions = {};
options = Object.extend(defaultOptions,(options || {}));
}

where options is a map {option1:value1, ...}

like image 31
Liviu T. Avatar answered Mar 16 '23 07:03

Liviu T.