The value of the array has changed even before changes were made

Update 2

I did another test in Chrome (latest version), it does not have this problem. Therefore, I assume this is fixed in Chrome, but becomes a problem in Firefox. interesting:)


Update

I ran my test in Firefox, which only updated a few hours ago (so it's not about lazy pricing in Chrome)


I searched if JavaScript is a password-link or forwarded language, got the answer from here Is JavaScript a password or default language?

Then I did a test that surprised me with its result:

var array = [1,2,3,4,5,6,7,8];

console.log(array);

change(array);
console.log(array);
function change(a)
{
    var t = a[a.length -1] + 10;
    var ta = a;
    console.log('ta = '+ ta);
    ta.push(t);
      console.log('ta1 = '+ ta);
}

Result:

[1, 2, 3, 4, 5, 6, 7, 8, 18] 
"ta = 1,2,3,4,5,6,7,8" 
"ta1 = 1,2,3,4,5,6,7,8,18" 
[1, 2, 3, 4, 5, 6, 7, 8, 18]

Here you can find your jsfiddle http://jsfiddle.net/hm9KN/

Can someone explain why the value of the array changed even before any changes were made to it?

+4

Source: https://habr.com/ru/post/1541085/


All Articles