What does { content: [Circular] } mean in JavaScript?

后端 未结 3 424
萌比男神i
萌比男神i 2020-12-17 16:18

My code was:

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

and it returned:

{ content: [Circular] }
相关标签:
3条回答
  • 2020-12-17 16:40

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

    0 讨论(0)
  • 2020-12-17 16:41

    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 讨论(0)
  • 2020-12-17 16:48

    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.

    0 讨论(0)
提交回复
热议问题