7

As my understand, Javascript pass object by reference, and a array also a object but when I create a array of integer then passing it to a function as below code:

function testFunc(outTestArray) {
  var aiTemp = [1,2,3,4];

  /*Using slice(0) to clone array */
  outTestArray = aiTemp.slice(0);
}

var aiTest = Array.apply(null, Array(4)).map(Number.prototype.valueOf, 0);
testFunc(aiTest);

console.log(aiTest.toString()); // aiTest still [0,0,0,0]

I also know that the slice(0) function just return a shallow copy of array, but in case the array is only a array of integer. So my question is why the data of aiTest is not modified?

Trung Nguyen
  • 818
  • 2
  • 10
  • 17
  • 1
    http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – MONTS_MIND_Hacker Mar 05 '16 at 04:35
  • 2
    Duplicate. Short answer: Javascript is always pass by value, EXCEPT for arrays/objects. – Max Mar 05 '16 at 04:35
  • 1
    JavaScript is always by-value (copy). Though, with objects, the value is a reference (reference-by-value). [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) What you're expecting is a pointer, where `outTestArray` refers back to `aiTest`, which JavaScript doesn't have. – Jonathan Lonowski Mar 05 '16 at 04:35
  • I thinks the problem is at slice(0) function. Because if in the function testFunc() i modify as follows: outTestArray[0] = 1; outTestArray[1] = 2;... then pass aiTest to this function, aiTest will be changed. – Trung Nguyen Mar 05 '16 at 04:57
  • @TrungNguyen The issue is the `=`. By assigning `outTestArray`, you modify its value. But, `aiTest` still refers to the original array. The reason `outTestArray[1] = 2` behaves differently is because that modifies the array itself rather than modifying either variable. – Jonathan Lonowski Mar 05 '16 at 05:37

3 Answers3

2

Function arguments are like new variable assignments, it's not like references or pointers in C. It's more something like this:

function testFunc(...args) {
  var outTestArray = args[0];
  var aiTemp = [1,2,3,4];

  /*Using slice(0) to clone array */
  outTestArray = aiTemp.slice(0);
}
...

As you can see, in the code above you are cloning the array and assigning it the variable outTestArray scoped within the function, which is not accessible outside of it.

You could use a closure to achieve what you want:

var outerArray;
function testFunc(array) {
  var aiTemp = [1,2,3,4];
  outerArray= aiTemp.slice(0);
}

or even better, just return a new array:

function getArray() {
  return [1,2,3,4];
}

var aiTest = getArray();
...
Ziarno
  • 7,366
  • 5
  • 34
  • 40
0

Your error is that your concept of a shallow copy is wrong.

A shallow copy copies the contents of the array, but does not copy the things referred to by array members.

If your array was an array of references to objects, a shallow copy would copy the references, so your new array would point to the same underlying objects.

But this array is ints. The ints themselves get copied by a shallow copy.

Chris Kitching
  • 2,559
  • 23
  • 37
  • As you say "The ints themselves get copied by a shallow copy." Yes I know this. But it is not related to why aiTest is not changed after out of function? – Trung Nguyen Mar 05 '16 at 04:49
  • 1
    The array is passed by _copy of reference_. testFunc's outTestArray is a reference to the underlying array. Your aiTemp assignment sets that _reference_ to point to the new array [1,2,3,4]. Which you then copy for some reason. You're not modifying the array behind the reference, you're modifying the _reference itself_ – Chris Kitching Mar 05 '16 at 04:52
  • To be clear, you could modify the underlying array by doing something that mutates it, like `outTestArray[0] = 1`. That would modify the first value of your `aiTest`. – Chris Kitching Mar 05 '16 at 04:53
  • 1
    So: Assignment modifies references. Objects are passed by copying reference. But you can still mutate objects by calling functions on them that update their state, or otherwise. – Chris Kitching Mar 05 '16 at 04:54
0

In your code, testFunc gets a reference to the original array which is copied into argument outTestArray. In the body of the function you are making outTestArray point to a different array. If you want to change the original array, then you could say

outTestArray.splice(0, outTestArray.length, ...aiTemp)

Note that spread operator is being used for aiTemp.