var root = this;root.node = function(path, type) {  root.type = type;  this.type = type;   this.parents = [];   this.children = [];   var components = path.split('/');   this.parents = parseParents(components);   this.name = components[components.length - 1];   this.addChild = function(path, type){              if(this.type === 'file'){                throw new Error('A file node cannot have any children');              }              this.children.push(new root.node(path, type));  }  function parseParents(components){        var parents = [];        if(components.length > 0){           for(var i = (components.length - 1); i > 0; i--){              if(components[i] !== )                if(parents.length === 0){                  var currentParent = new node(components[i], 'dir');                  currentParent.children.push(new node(components[0], root.type));                  parents.push(currentParent);                 }                else{                  var newParent = new node(components[i], 'dir');                  var curretnChild = new node(components[i - 1], 'dir');                  currentParent.parents.push(newParent);                  currentParent = newParent;                }           }           return parents;         }         return [];   }   return this;}var a = new node('/usr/local/foo', 'dir');a.addChild('test', 'file');console.log(a);2 way linked list, works great besides that the the 'leaf' items are not populated currently, since in the current structure it will result in an infinite loop.You thoughts please.
Directory tree representation with double linked list
javascript;linked list;file system
Not sure this question is valid, but there is reviewable code:node is a constructor, it should be named with an uppercase NcurretnChild is not properly spelled, and not used anywhere, drop itSince you set parents to [], you might as well return parents when if(components.length > 0){ is not trueYou should declare your vars on top in parseParents, the scope of newParent and currentParent is the whole function anywayYour reliance on root.type is a bug in waiting, find a better way to figure out what the type should be. addChild should be part of Node.prototype, it does not make sense for every instance of Node to have it's own version of this functionYour code in addChild does not take care of dupes, that should be addressedFor fun, I  wrote this, it creates a double linked list and takes most of my comments in to consideration, except for the last one ( does not take care of dupes ).function Node( path , type ){  var parts = this.cleanPathParts( path.split(/) ),      part  = parts.pop();  //Set the basics  this.name = part;  this.type = type;  //Do we still have parts left ?  if( parts.length ){    this.parent = new Node( parts.join('/'), this.DIR );    this.parent.children = [ this ];  }}//Ignore blanks and `..` stringsNode.prototype.cleanPathParts = function( parts ){  return parts.filter( function(part){    return !!part && part != ..;  });};//Get the ultimate parent, usefull for adding path childrenNode.prototype.getRoot = function(){  var root = this;  while( root.parent )    root = root.parent;  return root;};//Add a child, either a simple node or a pathNode.prototype.addChild = function( part , type ){  if( this.type != this.DIR ){      throw new Error('A file node cannot have any children');  }  var node = (new Node( part, type )).getRoot();  node.parent = this;  this.children = this.children || [];  this.children.push( node );};//ConstantsNode.prototype.DIR  = 'dir';           Node.prototype.FILE = 'file'; var a = new Node('/usr/local/foo', 'dir');a.addChild('1/2/test', 'file');a.addChild('test', 'file');console.log(a);