简体   繁体   English

JavaScript对象的子对象可以引用自身吗?

[英]Can a JavaScript object's child reference itself?

I have a JavaScript object Team and a Score which represent points and some other functions. 我有一个JavaScript对象Team和一个Score ,分别代表分数和其他一些功能。 I want to know if it's safe to store the team in the score at the same time as storing the score in the team. 我想知道在将分数存储在团队中的同时将团队存储在分数中是否安全。

var Score = function(team){
    this.team = team;
    this.points = 0;
    ...
}

var team = {
    name : 'Team 1',
}
team.score = new Score(team);

The result of this is that if I log team.score.team.score.team.score.team.score.team.score.points = 0. This is perfect for what I am programming, however does it represent a dangerous setup that may crash older browsers or cause any other issues? 这样的结果是,如果我登录team.score.team.score.team.score.team.score.team.score.points =0。这对于我正在编程的内容来说是完美的,但是它代表了危险的设置可能会使较旧的浏览器崩溃或引起任何其他问题? It looks exactly like an infinite loop however Chrome seems to be handling it fine. 看起来就像一个无限循环,但是Chrome似乎可以很好地处理它。

Are there any reasons why I shouldn't do this? 有什么原因我不应该这样做吗?

Good question by the way. 好的问题。

This is called circular referencing . 这称为循环引用

Meaning the you are creating the nested reference of the same object. 意味着您正在创建同一对象的嵌套引用。

Garbage collection in browsers: The main function of the garbage collector in the browser is to free the memory if the memory occupied by the object is no longer in use. 浏览器中的垃圾收集器:浏览器中垃圾收集器的主要功能是如果不再占用对象占用的内存,则释放内存。 But in the case of circular reference 但是在循环引用的情况下

An object is said to reference another object if the former has an access to the latter (either implicitly or explicitly). 如果前者可以访问后者(隐式或显式),则称该对象引用了另一个对象。 For instance, a JavaScript object has a reference to its prototype (implicit reference) and to its properties values (explicit reference) 例如,JavaScript对象具有对其原型(隐式引用)及其属性值(显式引用)的引用。

(Source MDN ) (来源MDN


This is forcing the garbage collecting algorithm to prevent the object from being garbage collected, which in turn is a memory leak. 这迫使垃圾收集算法阻止对象被垃圾收集,这反过来又是内存泄漏。

As per the MDN Mark and sweep algorithm is been improved in such circumstance of circular referencing which is intelligent enough to remove the object of this type. 根据MDN 在这种循环引用的情况下,对标记和扫描算法进行了改进,这种智能足以消除此类对象。

Circular referencing was a problem in IE < 8 which caused the IE browsers to go hay wire on this. 循环引用是IE <8中的一个问题,这导致IE浏览器对此产生了麻烦。 Read this link and this one 阅读此链接 ,这一个


IBM link IBM 链接

This article sheds light on JavaScript circular referencing memory leak with example and clarity on the subject. 本文通过示例和清晰的主题,阐明了JavaScript循环引用内存泄漏的方法。


Final Verdict: Better to avoid circular referenced objects, only use when its highly needed at programmers discretion. 最终判决:最好避免使用循环引用的对象,只有在程序员自行决定是否需要它时才使用。 As modern browsers today are quite efficiently built though but its not a good practice as a developer to write code that causes unwanted memory consumption and leaks. 尽管当今的现代浏览器的构建效率很高,但作为开发人员编写可导致不必要的内存消耗和泄漏的代码,这并不是一个好习惯。

var Score = function(team,point){
    this.team = team;
    this.points = 0;
    ...
}

var team = {
    name : 'Team 1',
   point : 'point'
}
team.score = new Score(team);
team.score = new Score(point);

Try this, maybe it can help you 试试这个,也许它可以帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM