在下面的代码中
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
它的意思是
link.source = nodes[link.source]
if nodes[link.source]
is 不是未定义的.
If nodes[link.source]
is 不明确的然后下面的块将被执行。
(nodes[link.source] = {name: link.source})//assigning new value to nodes[link.source]
并且上面的值将被设置为link.source
所以如果你把它变得简单的话,它会是这样的:
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
相当于:
if (!nodes[link.source]) {//not undefined
link.source = nodes[link.source];
} else {
nodes[link.source] = {name: link.source}
link.source = nodes[link.source];
}
希望这可以帮助!
对您的评论的解释
Question (a = b || c equates to a = b but if b is undefined make a = c, right?)
YES
Question仍然没有意义的是为什么这些分配的左侧是 link.source 和 link.target?那些已经定义了,它们是我们想要填充节点的内容?
是的!你在这里是对的Those are already defined
。
link.source 当前 = "A"
块执行后,每个 link.source 将指向一个对象,如下所示。link.source = {name:A}
如果您仍然感到困惑,请告诉我。