I've found several articles, both here on SO and elsewhere that mention this error message, but none that I can apply to my specific situation. Can anyone help me understand what's going on?
If I write my code like so:
selDocument.group_selected = function ($, anchor) {
let tmpitems = $(".chkSelectedField:checked");
if (tmpitems.length > 1) {
let items = [];
$(tmpitems).each(function (a, b) {
items.push($(b).val());
});
if (typeof anchor != 'undefined') {
items = items.filter(el => el != anchor);
} else {
anchor = items.shift();
}
console.log("57199516 items, anchor", items, anchor);
it works fine, but if I take those blocks and break them into their own function:
selDocument.group_selected = function ($, anchor) {
let tmpitems = $(".chkSelectedField:checked");
if (tmpitems.length > 1) {
let retval = selDocument.create_items_array($, tmpitems, anchor);
let items = retval.items;
anchor = retval.anchor;
console.log("57199516 items, anchor", items, anchor);
...
selDocument.create_items_array = function ($, tmpitems, anchor) {
let items = [];
$(tmpitems).each(function (a, b) {
items.push($(b).val());
});
if (typeof anchor != 'undefined') {
items = items.filter(el => el != anchor);
} else {
anchor = items.shift();
}
return JSON.parse({ 'items': items, 'anchor': anchor });
};
I get "Cannot access 'anchor' before initialization."