I have a bit of code on my site that is
$(function () {
for (var k = 0; k < ogmap.length; ++k) {
$('#orglist').append($('<option></option>').text(ogmap[k]['orgname']).val(ogmap[k]['orgname']));
} // create organization option memu from list of organizations
function updateInfo() {
var newlySelectedOrg = $('#orglist option:selected').val();
$('#currorg').text(newlySelectedOrg);
var categories = [];
for (var k = 0; k < ogmap.length; ++k) {
if (ogmap[k]['orgname'] == newlySelectedOrg) {
categories = ogmap[k]['catnames'];
break;
}
} // Get array of strings corresponding to the categories for the selected organization in
$('#catlist > option').each(function () {
$(this).remove();
}); // remove current <option> list items for the categories <select> list
for (var k = 0; k < categories.length; ++k) {
$('#catlist').append($('<option></option>').text(categories[k]));
} // add new <option> list items for the categories <select> list
}
updateInfo();
$('#orglist').change(function () {
updateInfo();
});
});
because I need to define the updateInfo
function and also run it because it's part of the preprocessing for my page. I know that
var updateInfo() = function() { ... }
or equivalently
function updateInfo() { ... }
define the function and don't run it, and
(function() { ... })
runs it but keeps it anonymous.
Is it possible to both define it and run it?
For some reason, having
function updateInfo() { ... };
updateInfo();
just rubs me the wrong way, gives me a sense that I'm not using the best practice.