Circular structure

2 posts / 0 new
Last post
Circular structure

I have been going through the tutorial and the code and it has been a great learning experience. I have found something and wonder if you could comment when you have a few minutes to spare! :)

Going through the book application in pure JavaScript, if I issue this command:

JSON.stringify(slots)

I get this:

"{"isbn":"0553345842","title":"The Mind's I","year":"1982","publisherIdRef":"Bantam Books","authorsIdRefToAdd":["3"]}"

However, If i try to use the book object:

JSON.stringify(book)

I get this error:

VM419:1 Uncaught TypeError: Converting circular structure to JSON(…)

Checking the runtime code with the debugger, as in the following screenshot,

I understand that we are using references to objects and so the circular reference: book ==> authors ==> authored books ==> book, etc.

Can this be a bad thing if we have a book published by a major publishing company with thousands of boos published?

Should we just use a ‘foreign’ key to the publisher instead of an object reference?

Thank you,
Everardo

Can this be a bad thing if we

Can this be a bad thing if we have a book published by a major publishing company with thousands of boos published?

No, that's not really creating any problem, since a reference is as efficient as it is, no matter how large the objects are, which are referenced, if this is your concern.

Should we just use a ‘foreign’ key to the publisher instead of an object reference?

We normally prefer using object references in OO programs since they allow direct (and thus more efficient) access. A foreign key (or ID reference) requires a two step access procedure: first resolve the ID into an object reference, then access the object.

In user-interactive CRUD operations, this difference in access efficiency may not matter, but if you would do more complex data analysis requiring to process a great many objects in main memory, then it would (and also in simulations and games).

-Gerd