14

My code was:

var chineseBox = {};
chineseBox.content = chineseBox;

and it returned:

{ content: [Circular] }

What does that mean?

5
  • This is not part of javascript (Ecmascript) - the chineseBox return a JSON object that specified that the content is Circular (whatever that means to the chineseBox) Oct 28, 2011 at 0:29
  • It returned that how? Something is missing here.
    – BNL
    Oct 28, 2011 at 0:29
  • 2
    The second line of code will evaluate to the rvalue, which is chineseBox. Just the same way chineseBox.content = 5 would evaluate to 5.
    – Peter
    Oct 28, 2011 at 0:34
  • Interesting aside: there's a proposal to add a property to the JavaScript global object which has the key "global" (so you'd access it with <global object name here>.global), and which returns the global object (a circular reference). Then, you'd be able to simply call global anywhere, and because every property of the JS global object is accessible by just its property name alone, asking for global would always return the global object no matter what the global object was actually named ('window', 'process', 'whatever' etc)
    – iono
    Sep 26, 2018 at 6:55
  • @chelsea how on earth did you get the chelsea twitter handle :D I am impressed. Nov 20, 2018 at 17:32

3 Answers 3

23

Your object contains a circular reference. If you tried to print (or serialize) this object you would end up in an infinite loop

{content: {content: {content ...

Instead, your system was smart enough to notice the circularity and protect itself against it.

2
  • Thank you! and @numbers1311407 this is literally what my terminal looked like: d.pr/nE8a Oct 28, 2011 at 0:41
  • 1
    Cool, that would be Node.js; which uses the V8 engine.
    – Peter
    Oct 28, 2011 at 0:43
9

It means that you have defined a circular reference. In other words, a variable that references itself.

Put another way, chineseBox is equal to chineseBox.content is equal to chineseBox.content.content is equal to chineseBox.content.content.content is equal to chineseBox.content.content.content.content is equal to chineseBox.content.content.content.content.content is equal to chineseBox.content.content.content.content.content.content is equal to chineseBox.content.content.content.content.content.content.content is equal to chineseBox.content.content.content.content.content.content.content.content is equal to chineseBox.content.content.content.content.content.content.content.content.content is equal to chineseBox.content.content.content.content.content.content.content.content.content.content is equal to chineseBox.content.content.content.content.content.content.content.content.content.content.content is equal to chineseBox.content.content.content.content.content.content.content.content.content.content.content.content is equal to chineseBox.content.content.content.content.content.content.content.content.content.content.content.content.content ... and so on ad infinitum.

Since such a circular reference quite clearly cannot be expressed in any finite manner, the result of [Circular] is shown. Other languages have their own phrase for the term, such as PHP which displays *RECURSION*. There's nothing particularly world-ending about circular references, just that they're pretty useless most of the time.

A back-and-forth reference, such as a.thing = b; b.thing = a; can be useful, though, for use in linked lists or other chains that you need to be able to navigate backwards as well as forwards - again, you can write a.thing.thing.thing.thing.thing.thing as much as you want.

0
3

Thats because chineseBox.content has a reference to chineseBox which is the same Object. A Circular reference...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.