javascript-allonge icon indicating copy to clipboard operation
javascript-allonge copied to clipboard

Ourouborean example is confusing

Open raganwald opened this issue 12 years ago • 1 comments

At the end of https://leanpub.com/javascript-allonge/read#leanpub-auto-val..., it states that when you do this:
  var ouroboros = [];
  ouroboros[0] = ouroboros;
    //=> [ [Circular] ]
Examining ouroboros and ouroboros[0] with '===' will show that they are different.
But isn't that wrong? Comparing a reference of 'ouroboros' to whatever's in 'ouroboros[0]', which happens to be a reference of 'ouroboros' will return true.
Maybe I'm misinterpreting the statement?
reply


anonymoushn 1 day ago | link

It looks like this text is about the section slightly above:
  [2-1, 2, 2+1] === [1,2,3]
  [1,2,3] === [1, 2, 3]
  [1, 2, 3] === [1, 2, 3]
reply


ajanuary 1 day ago | link

Aah, rereading it, it looks like it is. The ouroboros section is an aside about how confusing combining arrays and references can be.
reply


toki5 1 day ago | link

Just a guess -- perhaps
    ouroborous[0] = ouroboros;
creates a new reference to ouroboros and stores that in ouroboros[0], so comparing ouroboros and ouroboros[0] with === would claim they're the same type but not the same value.
Not an expert though, so I could be wrong, but that'd explain the behavior to me.
reply


roryokane 1 day ago | link

Yes, I don’t understand what he means either.
    ouroboros[0] === ouroboros
evaluates to `true`.
reply

raganwald avatar Oct 03 '13 11:10 raganwald

I'm reading this in Chrome, and it seems that the 'aside' sections don't have any css associated with them that would distinguish them from the regular flow of text. This is why this example is confusing.

fgshprd avatar Oct 04 '13 21:10 fgshprd