简体   繁体   中英

Javascript or D3js Notation

How is there an assignment operator in the OR function.

What would be the result of each of the two return statements.

Refer: http://www.d3noob.org/2014/01/tree-diagrams-in-d3js_11.html

var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });

nodeEnter.append("text")
.attr("x", function(d) { 
return d.children || d._children ? -13 : 13; })

in your example, usually it is the way used to define unique index in d3

var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
//here if d.id exists and is truthy, just return d.id; if not, then assign d.id to ++i

second is same:

if d.children is (defined and truthy value) then return -13
if d.children is not defined or is falsy then return 13

(For reference: "Truthy" and "Falsy" Values )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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