Skip to content

Instantly share code, notes, and snippets.

@madbook
Last active December 16, 2015 22:28
Show Gist options
  • Save madbook/5506730 to your computer and use it in GitHub Desktop.
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.
/**
* 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);
};
@lukebennett
Copy link

FYI you have a typo in your second example:

var bat = Object.create(bat);

Should be

var bat = Object.create(foo);

@madbook
Copy link
Author

madbook commented May 3, 2013

fixed, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment