Created
December 9, 2011 03:48
-
-
Save draeton/1450062 to your computer and use it in GitHub Desktop.
Nodes with children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Node ( val, parent ) { | |
this.value = val; | |
this.setParent( parent ); | |
this.children = []; | |
} | |
Node.prototype = { | |
addChild: function ( val ) { | |
var node = val instanceof Node ? val : new Node( val ); | |
node.setParent( this ); | |
this.children.push( node ); | |
}, | |
setParent: function ( node ) { | |
this.parent = node; | |
} | |
}; | |
var a = new Node( "a" ); | |
a.addChild("x"); | |
a.addChild("y"); | |
a.addChild("z"); | |
// the node | |
console.log( a ); | |
var kids = a.children; | |
// check parent | |
console.log( kids[0].parent === a ); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment