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

320
Visualizações
Is it possible to change the value of the function parameter?

I have one function with two parameters, for example function (a,b). I want to within the function, take the value of b, and replace that with c. I was thinking of something like $(b).replaceWith(c), but it didn't work out. I know I can create a new variable within the function with the new value, but is it possible to change the value of b itself? Is something like this possible. Or are the parameters set in stone?

Let me try to explain what I am trying to do. There are three functions, one overarching function and then two functions within it which are triggered by toggle event. And I want the second function to do something to get a value and pass it on to the third function. So here would be the code

function(a,b){

    $('selector').toggle(

        //I want this to gather a value and store it in a variable
        function(){},

        //and I want this to accept the variable and value from the previous function
        function(){}
    )}

The only way I can think of doing this is to add a parameter c for the overarching function and then modify it with the first function and have the new value pass on to the second function.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You cannot pass explicitly by reference in JavaScript; however if b were an object, and in your function you modified b's property, the change will be reflected in the calling scope.

If you need to do this with primitives you need to return the new value:

function increaseB(b) {
  // Or in one line, return b + 1;
  var c = b + 1;
  return c;
}

var b = 3;
console.log('Original:', b);
b = increaseB(b); // 4
console.log('Modified:', b);

over 4 years ago · Santiago Trujillo Relatório

0

You cannot change the value of an integer or a string in JavaScript BUT you can change the value of the attributes of an object for example

function change(x) {
  x.x = 2;
}

let y = { x: 1 };
console.log('Original:', y);

change(y); // will make y.x = 2;

console.log('Modified:',y);

over 4 years ago · Santiago Trujillo Relatório

0

When passing an object, you are passing a reference to that object. So when modifying an object that was passed into a function you are modifying that original object. Therefore you can create a very simple object containing a single variable that will allow you to carry the value around from one function to the next.

There is one thing that should be noted about the way you are putting these function calls into the parameter list of another function. I am not certain what the order of execution is for evaluating a parameter list. Perhaps someone who knows more about Javascript can answer that. However, not all languages will evaluate a parameter list left to right which means you may find the second function executes before the first. Again though, I'm not certain of the order of execution for Javascript so that may not be a concern.

function(a,b){
    var object_c = {
        c: 10
    };

    $('selector').toggle(

    //send object_c in to gather a value and store it in the "c" field
    function(object_c){},

    //send the same object_c object into the second function
    function(object_c){}
)}
over 4 years ago · Santiago Trujillo 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