-1

I need to make something like this, but I don't know how

var a = 5

function myFunction(x){
  if(x != 4) {
    x = 4;
  } 
}

myFunction(a);

//afterward, a should equal 4

The clue is, when this.x changes, var a should change too (I need to be able to make complicated operations on lots of variables and I want to do so by invoking a function)

Is it even possible?

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
  • You should probably read some about Closures in Javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures – Mathias W Apr 11 '18 at 19:03
  • 4
    What you are asking for is called "pass by reference", it means instead of passing the value of `a`, you want to pass a reference to `a` when you run the function. Sadly, this is impossible in js, according to this answer: https://stackoverflow.com/a/7744623/8759952 – Liora Haydont Apr 11 '18 at 19:04
  • 1
    While pass-by-reference doesn't exist in JS, you can reassign values. So you could simply use `return x` at the end of the function, and call it with `a = myFunction(a)`. Of course this doesn't handle your multiple variables scenario. For that, you might want to pass an object with multiple properties.... – Scott Sauyet Apr 11 '18 at 19:08
  • @SpencerWieczorek Your comment was true, I was over simplifying the idea, which was wrong of me. :) – Xotic750 Apr 11 '18 at 19:27

2 Answers2

1

Though it's not the same but you can do something similar.

var a = {value: 5};

function myFunction(x){

    if(x.value != 4){
        x.value = 4;
    }
}

myFunction(a);

Or with array:

var a = [5];

function myFunction(x){

    if(x[0] != 4){
        x[0] = 4;
    }
}

myFunction(a);

Also I would recommend using typesafe comparing (===), (!==)

0

When you call myFunction a copy of the value of x is passed to the function. So if you call it passing a, the numeric literal 5 would be passed there. As you have stated you want to alter the value of a, when the argument value you pass to myFunction is different from 4. You can do fairly easily by referring to the a directly in the body of your if statement. The runtime would first check in the function myFunction if a variable with name a has been defined. Since there isn't any variable a defined inside myFunction, it will look at the scope where myFunction is defined, global scope. Since there a is defined it would assign the value of 4.

var a = 5;

function myFunction(x){

    if(x != 4){
       a = 4;
    }   
}

myFunction(a);

console.log(a);
Christos
  • 53,228
  • 8
  • 76
  • 108
  • 1
    I think this is defeating the object of the OPs question, it looks more like a bad choice in the OPs design. – Xotic750 Apr 11 '18 at 19:07
  • Its not the point. When i call it for b, i want b = 4, and when called for c, c = 4 – Aleksander Hauzer Apr 11 '18 at 19:09
  • 1
    Unfortunately what you want to do is not going to work. Or it will in very specific circumstances and will be fragile, – Xotic750 Apr 11 '18 at 19:11
  • @AleksanderHauzer That you have to understand is that whatever you name the name of the variable you pass to the `myFunction`, this would be always a copy of the original value. passing there the copy of this value you can't alter the original value. – Christos Apr 11 '18 at 19:12