I’m passing a large “options” object to another set of objects I’m instantiating in JavaScript. The problem is, a very few of these “options” must change from object to object. Making a completely separate options variable, with 1 of the many options changed, feels silly. I also don’t think I can just change the option on the same “options” object, as all of the objects will reference the same “options”.
Below is the relevant code.
for (var i = 0; i < invoices.length; i++) {
var ura_original_column = { "column" : "ura_ppa_original",
"on_update" : [format_ura],
"display" : "URA" };
if (invoices[i]["type"] == "P") {
ura_original_column = { "column" : "ura_original",
"on_update" : [format_ura],
"display" : "URA" };
}
var options = { template_table : "template_table",
template_total : "template_total",
template_row : "template_row",
template_text : "template_text",
template_select : "template_select",
packet_id : <?val=packet["packet_id"]?>,
products : <?val=json.dumps(products)?>,
allow_new_rows : <?val=json.dumps(packet["status"] not in api.NON_MODIFIABLE_STATUS)?>,
on_table_focus : on_table_focus,
on_row_update : on_row_update,
on_new_row : on_new_row,
columns : [{"column" : "product_code",
"display" : "Product"},
{"column" : "transaction_type",
"display" : "FFSU/MCOU",
"editor" : "selectedit",
"options" : ["FFSU", "MCOU"]},
ura_original_column,
{"column" : "ura_current",
"display" : "Calculated URA"},
{"column" : "units_current",
"display" : "Current Units",
"on_update" : [format_units],
"show_total" : true},
{"column" : "amount_claimed",
"display" : "Amt Claimed",
"on_update" : [format_currency],
"show_total" : true},
{"column" : "scripts_current",
"display" : "Scripts",
"on_update" : [format_scripts],
"show_total" : true},
{"column" : "amount_medi_reimbursed",
"display" : "MEDI Amt",
"on_update" : [format_currency],
"show_total" : true},
{"column" : "amount_non_medi_reimbursed",
"display" : "Non-MEDI Amt",
"on_update" : [format_currency],
"show_total" : true},
{"column" : "amount_total_reimbursed",
"display" : "Total Amt",
"on_update" : [format_currency],
"show_total" : true}]}
var invoice_id = invoices[i]['invoice_id'];
var transactions = transactions_by_invoice[invoice_id];
var table = new Table.Table("invoice_" + invoice_id, options, transactions);
tables.push(table);
}
});
So, out of this gigantic options structure, only the “ura_original_column” changes. This might be the best way to do it, but it feels like a bit of a hack.
Anyone have a more elegant suggestion?
Thanks for taking the time to look.
You can use the new
Object.createto create a new object that only has the different option, but is backed by a prototype with all the other options. (This is an ES5 feature, but you can create a version of it that offers the main functionality or use on of the “ES5 shim” projects that does, including the bit you need; it’s impossible to fully createObject.createin a pre-ES5 environment, but you don’t need all of it.)That looks like this:
What you end up with is an object that only has the properties you changed, but which if asked for any of the other properties, will return the value from the main options prototype.
Here’s a self-contained example of doing this: Live copy | source
Again, important to understand that the
objectCreategiven there ifObject.createdoesn’t exist is not a full shim for the realObject.create. It’s just enough to get the bit we want done, done.