Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
"can't access variable before initialization" if I break the code into separate function

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."

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda