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

590
Visualizações
ESLint complains if I assign a function parameter, but not if I run a function that changes the function parameter. Why?

The following code selects all div elements and changes their class to 'test. ESLint complains about assignment to property of function parameter:

  [...document.getElementsByTagName('div')].forEach(div => {
    div.className = 'test';
  });

However, if I mutate the parameter by running a function or method, then I get no complaints. The following code does the exact same thing as the code that gets complained about, but is less efficient and less readable:

 [...document.getElementsByTagName('div')].forEach(div => {
    div.classList.remove(...div.classList);
    div.classList.add('test');
  });

Why is one okay but the other is not?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

ESLint's no-param-reassign rule includes prohibiting assignment of function arguments' properties. But it has a props option that you could configure to false if you want to keep the rule more generally but also allow it to mutate function arguments without reassigning them.

As the documentation page explains, the rule exists because reassigning (or mutating) function arguments will also mutate the arguments object, which may be confusing.

There's no way for ESLint to know if an object's method is going to mutate that object, which is why it can't error when you call a method that mutates a function argument.

In my experience, this side effect of mutating arguments is pretty rarely an issue, especially when you're just mutating function arguments instead of reassigning them. What you're doing in this example clearly isn't going to cause the sort of issue this rule is meant to help with.

Personally, this is a rule that I turn off in my projects.

about 4 years ago · Juan Pablo Isaza Relatório
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