Passing a value by reference

A

Archos

I've a function in Go which uses a value by reference, and the output
is 4:

===
func byReference() {
x := 3
f := func(){
x = 4
}
y := &x

f()
println(*y) // 4
}
===

How would it be in JS? I've tried:

===
function byReference() {
var x = [3];
var f = function() {
x = 4;
};
var y = x;

f();
console.log(y[0] + "\n");
}
===
but it outputs 3.

I've followed this rule: `*x` is `x[0]` in javascript while `x` would
simply be `x`. For any value that is addressed, it is treates as if
it were boxed in an array.
 
G

Gene Wirchenko

I've a function in Go which uses a value by reference, and the output
is 4:

===
func byReference() {
x := 3
f := func(){
x = 4
}
y := &x

f()
println(*y) // 4
}
===

How would it be in JS? I've tried:

[snip]

You could create an object and put the value in a property of
that, then pass the object to the function:

***** Start of Sample Code *****
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>2012-01-13: Parameters</title>
</head>

<body>

<script type="text/javascript">

function ByReference(InitValue)
{
this.value=InitValue;
}

function Increment(theByReference)
{
theByReference.value++;
}

var Thing=new ByReference(5);
alert("Thing.value before="+Thing.value);
Increment(Thing);
alert("Thing.value after="+Thing.value);
Increment(Thing);
alert("Thing.value after second="+Thing.value);

</script>

</body>

</html>
***** End of Sample Code *****

This will give results of 5, 6, and 7.

Sincerely,

Gene Wirchenko
 
A

Archos

I've a function in Go which uses a value by reference, and the output
is 4:
===
func byReference() {
   x := 3
   f := func(){
           x = 4
   }
   y := &x
   f()
   println(*y) // 4
}
===
How would it be in JS? I've tried:

[snip]

     You could create an object and put the value in a property of
that, then pass the object to the function:

***** Start of Sample Code *****
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
  <title>2012-01-13: Parameters</title>
</head>

<body>

<script type="text/javascript">

function ByReference(InitValue)
  {
  this.value=InitValue;
  }

function Increment(theByReference)
  {
  theByReference.value++;
  }

var Thing=new ByReference(5);
alert("Thing.value before="+Thing.value);
Increment(Thing);
alert("Thing.value after="+Thing.value);
Increment(Thing);
alert("Thing.value after second="+Thing.value);

</script>

</body>

</html>
***** End of Sample Code *****

     This will give results of 5, 6, and 7.

Sincerely,

Gene Wirchenko

It is not valid for me since I need a line to line transformation.
Thanks anyway!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top