0

I'd like to know if it is posible, having one or more variables to pass them into a function and get those variables modified. I think it is posible with objects, as they behave as references, but I don't know.

With 1 var you could do:

var something = increase(something);

but what if you have, for example, 2 variables with recipies, and would like to exchange them?

exchange_recipies(book1, book2);

You could do this but the variables are parameters inside the function... So is there another way that I'm missing?

EDIT: I know it can be done in many ways but I'll state some here that I don't like due to obvious limitations:

-Use global variables.

-Use objects

-Return an array and reasign.

EDIT2: this Is it possible to change the value of the function parameter? helped me, but I think that answer is uncomplet and there are ways of doing this.

Community
  • 1
  • 1
Vandervals
  • 5,774
  • 6
  • 48
  • 94
  • you could use, inside you function, window.book1 and window.book2 but I would not recommend that. Instead you could return an Array with 2 indexes or an Object with the function and re-assign the variable. http://jsbin.com/nayitimoso/1/edit?js,console – fatrex Jun 10 '15 at 13:22
  • I knew that, but I'm asking for a better way. The window solution only works for global variables, and returning an array would make the code less elegant – Vandervals Jun 10 '15 at 13:27
  • 2
    You're asking for pass-by-reference -- you've shot down all the existing ways to achieve something like the desired behavior. No amount of shouting "useless!" and "obviously!" is going to change that, sorry. – Paul Roub Jun 10 '15 at 14:05
  • possible duplicate of [Is it possible to change the value of the function parameter?](http://stackoverflow.com/questions/5292159/is-it-possible-to-change-the-value-of-the-function-parameter) – Paul Roub Jun 10 '15 at 14:05

2 Answers2

0

One way to do this is pass these two parameters as objects:

exchange_recipies({ obj: book1 }, { obj: book2 });

and then inside finction you can operate on obj properties

function exchange_recipies(book1, book2)
{
   book1.obj = book2.obj;
   ...
}

http://codepen.io/anon/pen/eNRZZb

suvroc
  • 3,058
  • 1
  • 15
  • 29
  • Well its not the -only- way, could also be done with the array if its just a simple list of variables but its certainly the best way for anything more complicated than that. – FroboZ Jun 10 '15 at 13:20
  • Well obviously! But I already said it could be done that way in my question from the begining! – Vandervals Jun 10 '15 at 13:37
0

If the variables are declared globally then you wouldn't need to pass them into the function since they can be addressed inside the function.

var something = 1
function bar(){
    something = increase(something);
}

For example.

This means you avoid changing the variables into parameters and can then address them from any function (dependant on nesting, ergo, if the variable is declared inside a function it can't be addressed outside of that function but can from a function inside)

function foo(){
    var something = 1
    function bar(){
        //works because it exists in the parent function
        something = increase(something)
    }
}

function foobar()
    //Something doesn't exist here so would return something = undefined
    something = increase(something)
}
  • This is useless, because the functions you are creating aren't reusable – Vandervals Jun 10 '15 at 13:43
  • The OP didn't clarify how he wanted the functions to be used, just that he wanted to change variables inside of a function. This has now been amended, in which case this isn't totally valid to the usecase. – Sgt Oddball Jun 10 '15 at 14:15
  • @Vandervals also as an aside where did the OP state that the functions (any of them) should be reuseable? I was only making the point that functions can be nested inside of each other and variables declared inside a function can then be modified by nested functions once more. – Sgt Oddball Jun 10 '15 at 14:19