raganwald / javascript-allonge

Markdown source for the book "JavaScript Allongé"
http://leanpub.com/javascript-allonge
759 stars 135 forks source link

Ourouborean example is confusing #19

Open raganwald opened 10 years ago

raganwald commented 10 years ago
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
fgshprd commented 10 years ago

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.