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

392
Visualizações
Is there anything wrong/dangerous about reassigning function parameters?

Is there anything I should be worried about if I write code like below. I was always told that reassigning function params was a big no-no in Javascript.

function getAddress(address, lowerCase) {
  address = { ...address }

  if (lowerCase) {
    address.line1 = address.line1.toLowerCase();
    address.city = address.city.toLowerCase();
  }

  return address;
}

Examples of advice:

  • https://eslint.org/docs/rules/no-param-reassign
  • https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

From my point of view, If address is an complex data type like object or an array, changing it in the function would mean changing the original object which might not be the intention or needed and introduce a bug

If the param is a primitive data type, it would create a copy in the scope of the function..so it should not be a problem

about 4 years ago · Juan Pablo Isaza Relatório

0

As I understand it, the reason why some places recommend not reassigning function parameters is that doing so will also change the arguments object, which is not necessarily expected behaviour. For functions that don't use arguments, however, this should not be an issue.

For example, consider the following:

function getAddress(address, lowerCase) {
    address = { ...address };

    var originalAddress = arguments[0];

    // Do stuff with address
}

For someone not familiar with how JavaScript works in this respect, it might be reasonable to assume that setting originalAddress like this would record the original first argument that was passed to the function. But actually, when address is reassigned, this also mutates the relevant record in arguments.

Even when you're not using the arguments object, it can be a useful practice to consider all arguments to a function to be constant and immutable, to avoid potentially introducing bugs such as through modifying an object that is passed to a function. In your example, this could be achieved like this:

function getAddress(addressArg, lowerCase) {
  const address = { ...addressArg }

  if (lowerCase) {
    address.line1 = address.line1.toLowerCase();
    address.city = address.city.toLowerCase();
  }

  return address;
}

In your implementation, so long as address only contains primitive properties (as opposed to properties that point to a reference value such as an object or an array) then your copying it through { ...address } should create a completely separate copy. However, this method only creates a shallow copy.

If address does contain object or array properties, but no properties that are classes or functions, then you could create a deep copy through JSON.parse(JSON.stringify(address)) instead.

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