最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Does Javascript pass array to function by reference or value? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Mar 5, 2016 at 4:32 Trung NguyenTrung Nguyen 8682 gold badges11 silver badges17 bronze badges 5
  • 1 stackoverflow./questions/518000/… – MONTS_MIND_Hacker Commented Mar 5, 2016 at 4:35
  • 2 Duplicate. Short answer: Javascript is always pass by value, EXCEPT for arrays/objects. – Max 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. – Jonathan Lonowski 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. – Trung Nguyen 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. – Jonathan Lonowski Commented Mar 5, 2016 at 5:37
Add a ment  | 

3 Answers 3

Reset to default 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();
...

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.

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.

发布评论

评论列表(0)

  1. 暂无评论