Explain Mike Bostock Node-Parsing Loop [duplicate]

五迷三道 提交于 2019-12-01 20:30:32

In the code below

link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});

It means

link.source = nodes[link.source]

if nodes[link.source] is not undefined.

If nodes[link.source] is undefined then block below will get executed.

(nodes[link.source] = {name: link.source})//assigning new value to nodes[link.source]

and the above value will be set to link.source

So if you make it simple it would be like:

link.source = nodes[link.source] || (nodes[link.source] = {name: link.source}); is equivalent to:

if (!nodes[link.source]) {//not undefined
 link.source = nodes[link.source];
} else {
 nodes[link.source] = {name: link.source}
 link.source = nodes[link.source];
}

Hope this helps!

Explanation for your comment

Question (a = b || c equates to a = b but if b is undefined make a = c, right?)

YES

Question What still doesn't make sense is why the left side of these assignments are link.source and link.target?Those are already defined, they're what we want to populate nodes with?

Yes! you are correct here Those are already defined. link.source is currently = "A" After the block executes each link.source will be pointing to an object, something like this. link.source = {name:A}

Let me know if you still have confusion.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!