2

While going through some code in d3.js to create a tree I found the symbol '_' being used and I have no what it is doing and have had no concrete solutions. The question I was looking at is this How to highlight path from root to selected node in d3 js?

The full code of the program I was referencing is this http://plnkr.co/edit/SKVV4A7a9bV0bZupS9M2?p=preview

I cant understand how the flatten function works especially why _ is used

function flatten(root) {
  var nodes = [],
    i = 0;

  function recurse(node) {
    if (node.children) node.children.forEach(recurse);
    if (node._children) node._children.forEach(recurse);
    if (!node.id) node.id = ++i;
    nodes.push(node);
  }

  recurse(root);
  return nodes;
}

Any help is appreciated thank you.

  • 4
    Don't know specifically for *d3.js*, but in JavaScript this `_` would be just part of the name of the variable/property. That's it, the name is `_children`. There would be a property in the `node` object called `children` and another called `_children`. – lealceldeiro Jul 12 '19 at 14:58
  • Have a look at the question of which this is a duplicate. The underscore—by convention—denotes a private property. In your example it is used to store a copy of the original `children` property to later be able to restore it to its original value. The main logic happens in the function `collapse()`. – altocumulus Jul 12 '19 at 15:03
  • @lealceldeiro i cant find anything called _children in the data files(full code given feel free to check i might have missed something). – Rushikesh Mokashi Jul 12 '19 at 15:07
  • @altocumulus Hello could you link me some documentation or an example i still cant grasp why would we need duplicates stored or private/public properties as i am very new to js. – Rushikesh Mokashi Jul 12 '19 at 15:09
  • 1
    @RushikeshMokashi There is a chapter [*An interactive tree diagram*](https://leanpub.com/d3-t-and-t-v4/read#leanpub-auto-an-interactive-tree-diagram) in Malcolm Maclean's book [*D3 Tips and Tricks v4.x*](https://leanpub.com/d3-t-and-t-v4) covering this topic. – altocumulus Jul 12 '19 at 15:19
  • @altocumulus the reference you provided guided me further to https://stackoverflow.com/questions/20940896/what-does-the-syntax-d-children-d-children-stand-for-in-d3-js/20940920 which exactly answers my question (which you even seem to have edited lol). Could you change the duplicate to this link as i feel this link answered my question better than the one you have currently stated. Also can you post this as the official answer so i can mark it correct. Thank you so much! – Rushikesh Mokashi Jul 12 '19 at 15:50
  • Good find! I knew I had seen this before, but couldn't find it as I had not written an answer to that question. I added it as a more specific dupe target while still retaining my original guess which is more general, yet answers your question. I consider it valuable spreading the knowledge of the underscore naming convention. – altocumulus Jul 12 '19 at 16:10

0 Answers0