News for hackers. Stuff that matters.
Re:Go 1.1 is released by cma on Tuesday May 14, 2013 @02:27AM (#5703262)
If you make a change to an integer that was in a variable and passed in by parameter, does Javascript really make the change to the caller's variable?
Re:Go 1.1 is released by eonwe on Tuesday May 14, 2013 @10:05AM (#5704875)
No. The scenario you posit would be true if JavaScript were call-by-reference.

JavaScript, however, is call-by-value. Primitive types (string, number, boolean) are passed to functions by copying their value, and reference types (such as any object you create) are passed by copying the value of their reference.

The reference semantics can be observed in JavaScript in some cases, such as:

  var i = 0;
  for (i = 0; i < 10; i++) {
    setTimeout(function() {
      document.write("i is now " + i);
    }, 10);
  }
Here the inner function gets reference of the variable i instead of its value.
Re:Go 1.1 is released by cynwoody on Tuesday May 14, 2013 @03:13AM (#5703402)
No.

However, if you pass an object or an array, the called function can change a field in the object or an element in the array (the difference between those two is an optimization), and the change will be visible to the caller.

Go, however, lets you pass in an explicit pointer to an int, through which the function can change the int to which the caller pointed. Of ourse, the caller can also change the passed-in pointer if it wants, but that has no effect on the caller's variable.

Re:Go 1.1 is released by the_gipsy on Tuesday May 14, 2013 @08:23AM (#5704386)
Javascript primitives (number, string, boolean) are always copied, the remaining (object, function) are passed by reference (arrays are objects). Similar to Java.