8

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?

5
  • 1
    stackoverflow.com/questions/518000/… Commented Mar 5, 2016 at 4:35
  • 2
    Duplicate. Short answer: Javascript is always pass by value, EXCEPT for arrays/objects. Commented Mar 5, 2016 at 4: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? What you're expecting is a pointer, where outTestArray refers back to aiTest, which JavaScript doesn't have. Commented Mar 5, 2016 at 4: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. Commented Mar 5, 2016 at 4: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. Commented Mar 5, 2016 at 5:37

3 Answers 3

1

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();
...
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

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?
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
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.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.