4

Update 2

I did another test in Chrome(the latest version), it doesn't have this issue. So I guess it's fixed in Chrome, but becomes an issue in Firefox. interesting :)


Update

I did my test in Firefox which just updated a few hours ago.(So it's not about the lazy evaluation in Chrome)


I was searching for whether JavaScript is a pass-by-reference or pass-by-value language, got the answer from here Is JavaScript a pass-by-reference or pass-by-value language?

Then I did a test which surprised me by 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);
}

The result is:

[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]

You can find its jsfiddle at here http://jsfiddle.net/hm9KN/

Can anyone explain why the value of array changed even before I make any changes to it?

Thank you

Community
  • 1
  • 1
Franva
  • 6,565
  • 23
  • 79
  • 144
  • I get the expected output: 1,2,3,4,5,6,7,8 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 – Evan Knowles May 19 '14 at 09:08
  • 1
    The problem is with the browser console displaying the latest value. Check this http://jsfiddle.net/hm9KN/1/ – techfoobar May 19 '14 at 09:08
  • @Evan Knowles: how about this http://jsfiddle.net/SV5P7/ ? + Note the small `i` icon at the `Object` – zerkms May 19 '14 at 09:12
  • Yes, I get the same console issue with that one. – Evan Knowles May 19 '14 at 09:16
  • @zerkms hi, I did my test in Firefox which just updated a few hours ago. – Franva May 19 '14 at 09:16
  • @techfoobar hi, great to know. Does it mean that the issue only affects when trying to use console.log, there will be no affects if I just do the process without outputing the value? – Franva May 19 '14 at 09:20
  • @zerkms so it's not the Chrome issue. but the Firefox issue, interesting haha – Franva May 19 '14 at 09:26
  • 1
    @Franva: it's the naive implementation. No wonders different developers decided to implement it in the same way. Btw, check http://jsfiddle.net/SV5P7/ So it's not "fixed". – zerkms May 19 '14 at 09:26
  • It does happen in my firefox too, probably a browser bug. – simonzack May 19 '14 at 09:28
  • It works fine for me in both IE and Chrome. It depends on when you open the console. If the console is opened after running the script, it shows the last value. Open the console beforehand, or just clear the console and then run the script, it shows the values just fine. – Abhitalks May 19 '14 at 09:33
  • Maybe because `array` is a reserved word, just change it to something else and it works fine, check [JSFidle](http://jsfiddle.net/hm9KN/5/) – Alireza Ahmadi May 19 '14 at 09:35
  • Guys, could it be the object gets updated in the console even after the console.log executes? In @zerkms example( http://jsfiddle.net/SV5P7/ ), try console.log(a.a.b) at the same spot http://jsfiddle.net/SV5P7/2/ – alou May 19 '14 at 09:38
  • 1
    @Franva - Its a problem with the console's buffer/flush implementation or related. This shouldn't cause any "bugs" in your application. – techfoobar May 19 '14 at 09:38
  • 1
    Hi @AlirezaAhmadi I tried your code in Firefox, problem persists, but it works fine in Chrome. So I can confirm that it's a Firefox bug. – Franva May 19 '14 at 09:38
  • hi @techfoobar glad to hear what you said :) I was stuck at a javascript bug for days and the most speechless part of it is I don't even know how to describe it because of its complexity. – Franva May 19 '14 at 09:40

0 Answers0