Is circular reference between objects a bad practice?

后端 未结 3 934
情歌与酒
情歌与酒 2021-02-18 13:27

I have a Model that will \"carry\" (Model.validator) a validator instance with it, and I need the Validator to have access to the Model\'s attributes. So, what I have come up wi

相关标签:
3条回答
  • 2021-02-18 13:45

    It will not be a problem for garbage collection: any new Garbage Collector (>IE6) will handle circular references just fine!

    It might be a problem though if you are doing recursive functions, or printing the object.

    So the answer is: it is no problem unless you screw up yourselves :-)

    0 讨论(0)
  • 2021-02-18 14:03

    There will not be any problems I'm sure. The most browsers' JS parsers can work with cycle dependecnies while garbage collecting. No more potential issues here.

    0 讨论(0)
  • 2021-02-18 14:04

    This kind of code will not cause memory leaks with today's browsers; as mentioned on MDN all major browsers have been shipping with mark-and-sweep GCs (that can handle cycles just fine) for some time now (e.g. Firefox itself has had a cycle collector since version 3).

    From an architectural standpoint, this kind of code introduces moderately tight coupling between the two objects (if one changes in even a minor way, the other needs to be reviewed to determine if it needs to change as well) and should consequently be avoided if possible. But there is nothing inherently wrong with it.

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