1

I have three variables

var r1 = 12;
var r2 = '';
var r3;

I need to make a function convert() with three parameters as so:

function convert(arg1, arg2, arg3) {
    // function body here
}

such that after I run this code

convert(r1, r2, r3)
console.log(r1, r2, r3)

I should get the output:

r2, "empty string", "undefined value"

That is it should changed the values of r1, r2 and r3 to the above values.

Code I tried:

function convert(arg1, arg2, arg3) {
  if (arg2 === '') {
    arg2 = "empty string";
  }
  if (arg3 === undefined) {
    arg3 = "undefined value";
  }
}

and then call convert(r1, r2, r3) it fails (obviously!) because of arguments are passed as values. But I need to know is there any way to make this function? Also, I have seen this question, so I know about how they are passed, but still, is there any way I can make this work?

Community
  • 1
  • 1
Razort4x
  • 3,296
  • 10
  • 50
  • 88
  • I think that question is clear: due to the semantics of parameter passing, it is not possible. You could pass in an object in which case changes to its properties would be visible after the function returns, but that's about it. – Jon May 31 '13 at 07:43
  • possible duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Jon May 31 '13 at 07:45
  • @Jon: No it's not a duplicate of that. – Razort4x May 31 '13 at 08:24

4 Answers4

0

You can't do this in JavaScript. It doesn't have pass-by-reference semantics.

You can use an intermediate object fo this, but seems that's not what you are looking for:

var data = {r1: 12, r2: '', r3: undefined};
function convert(data, field1, field2, field3) {
    // …
}
kirelagin
  • 13,248
  • 2
  • 42
  • 57
0

You could do something like this, but most likely something is flawed with your logic.

function convert(arr) {
    for (var i = 0, l = arr.length; i < l; i++) {
        // do your stuff here on arr[i]
    }
    return arr;
}

var a = convert([r1, r2, r3]);
//a now holds the right values, do whatever you want with them
//you can even load them back to the variables if that's what you really want
//r1 = a[0]; etc.

I don't know what is the reason to do this, where you want to use these new values, but it would be more clever to write the convert function for only one value, and call it when it's necessary.

function convert(val) {
    if (val === '') {
        return 'empty string';
    }
    if (typeof val === 'undefined') {
        return 'undefined value';
    }
    return val;
}

console.log(convert(r1));
kapa
  • 77,694
  • 21
  • 158
  • 175
0

In the unlikely event that you only want to convert global variables, you can just pass in the names of those variables:

function convert(arg1, arg2, arg3) {
  if (window[arg2] === '') {
    window[arg2] = "empty string";
  }
  if (window[arg3] === undefined) {
    window[arg3] = "undefined value";
  }
}

var r1 = 12;
var r2 = '';
var r3;
convert('r1', 'r2', 'r3');
console.log(r1, r2, r3);
Neil
  • 54,642
  • 8
  • 60
  • 72
-1

working code: fire the execute function on the event you need


function execute(){
    var r1 = 12;
    var r2 = '';
    var r3;
    convert(r1,r2,r3);
}

function convert(arg1, arg2, arg3) {
  if (arg2 === '') {
    arg2 = "empty string";
  }
  if (arg3 === undefined) {
    arg3 = "undefined value";
  }

  console.log(arg1,arg2,arg3);
//    console.log(arg2);
  //    console.log(arg3);

}