简体   繁体   English

JavaScript表达式中的多个分配和语法说明

[英]Multiple Assignments in JavaScript Expression and Syntax Explanation

The following code is from Mike Bostock's Force-Directed Graph with Mouseover . 以下代码来自Mike Bostock的带鼠标悬停力导向图 I am sorry that I can't ask a more pointed question about this code, but I was wondering if someone could explain the syntax in the forEach block that computes the distinct nodes from the links. 很抱歉,我不能对此代码提出更尖锐的问题,但是我想知道是否有人可以解释forEach块中的语法,该块计算来自链接的不同节点。

What exactly is happening in the line below and what is being assigned to what variables? 下面的行中到底发生了什么,将什么赋给了哪些变量? Is there a name for performing multiple assignments in the same expression? 在同一个表达式中有执行多个分配的名称吗?

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

var links = [
  {source: "Ericsson", target: "ZTE", type: "suit"},
  {source: "Kodak", target: "Samsung", type: "resolved"},
  {source: "Apple", target: "Samsung", type: "suit"},
  {source: "Kodak", target: "RIM", type: "suit"},
  {source: "Nokia", target: "Qualcomm", type: "suit"}
];

var nodes = {};

// Compute the distinct nodes from the links.
links.forEach(function(link) {
  link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
  link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});

There's actually a fair amount to unpack in this line: 在这一行中实际上有很多需要解压的东西:

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

First it should be mentioned that x = y || z 首先应该提到x = y || z x = y || z is equivalent* to this: x = y || z等效于*:

if (y)
    x = y;
else
    x = z;

It should also be noted that i = (j = k) means that we're going to assign k to j , and then assign that value to i as well. 还应注意, i = (j = k)意味着我们将k分配给j ,然后也将该值分配给i So in the end, i , j , and k will all have the same value. 因此,最后, ijk都将具有相同的值。

So in this case, we're really getting this: 因此,在这种情况下,我们确实得到了:

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

That is, if nodes[link.source] is truthy (not 0, null, undefined, empty string), then link.source will be assigned nodes[link.source] as a value. 也就是说,如果nodes[link.source]为真(不是0,null,未定义,空字符串),则将为link.source分配nodes[link.source]作为值。

If it is falsy, then both link.source AND nodes[link.source] will be assigned {name: link.source} as their value. 如果是虚假的,则将link.sourcenodes[link.source]都分配为{name: link.source}作为其值。


*Those are equivalent because when you're evaluating an || *这是等效的,因为当您评估|| , if the left hand side is truthy, there is no need to evaluate the right hand side--you can already evaluate the statement as true. ,如果左侧是正确的,则无需评估右侧-您已经可以将语句评估为true。 So in JavaScript, it "returns" y if y is truthy, z if y is falsy but z is truthy, or false if both are falsy. 所以在JavaScript中,“收益” y如果y是truthy, z如果y是falsy但z是truthy,或false如果两者都falsy。

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

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