Last active
December 16, 2015 22:28
-
-
Save madbook/5506730 to your computer and use it in GitHub Desktop.
Simple constructor function for creating tree-like object structures. Useful if you need simple inheritance on a tree-like data structure.
This file contains hidden or 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
/** | |
* every NodeObject's prototype is another NodeObject. The lowest level | |
* NodeObject's prototype is the NodeObject prototype. | |
* | |
* Each NodeObject has two default properties | |
* {NodeObject} parent : reference to the NodeObject that created this, also | |
* will be this NodeObject's prototype | |
* {NodeObjects[]} childNodes : every node created from this node using the | |
* create method will be added to this array | |
* | |
* these properties are non-enumerable, so the resulting objects are safe | |
* to enumerate over in for loops | |
*/ | |
function NodeObject (attributeList) { | |
var node = Object.create(this, { | |
parent: { value: this }, | |
childNodes: { value: [] } | |
}); | |
var key; | |
if (attributeList instanceof Object) | |
for (key in attributeList) node[key] = attributeList[key]; | |
return node; | |
} | |
// create a child node. The node that calls this method will be the new | |
// node's prototype | |
Object.defineProperty(NodeObject.prototype, 'create', { | |
value: function (attributeList) { | |
var child = NodeObject.call(this, attributeList) | |
this.childNodes.push(child); | |
return child; | |
} | |
}); | |
// creates a new root node. Its necessary to call the NodeObject | |
// constructor this way for a root-level node, so that it's prototype is | |
// the NodeObject prototype and not the window | |
NodeObject.createRoot = function (attributeList) { | |
return NodeObject.call(NodeObject.prototype, attributeList); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fixed, thanks!