Skip to content

Instantly share code, notes, and snippets.

@videlais
Created May 2, 2014 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save videlais/7fcdcf6ce1f693ef3290 to your computer and use it in GitHub Desktop.
Save videlais/7fcdcf6ce1f693ef3290 to your computer and use it in GitHub Desktop.
JS Lesson 10
function Node(name) {
this.name = name;
this.childNodes = new Array();
}
Node.prototype = {
_lastChild: function() {
if (this.childNodes.length > 0) {
return this.childNodes[this.childNodes.length - 1];
}
}
};
Node.prototype = Object.create(Node.prototype);
Node.prototype.constructor = Node;
function Element() {
Node.call(this);
this.childNodes.push(new Node('lastChildNode'));
}
Element.prototype = Object.create(Element.prototype);
Element.prototype.constructor = Element;
Object.defineProperty(Element.prototype, 'lastChild', {
enumerable: true,
get: function() {
return Node.prototype._lastChild.call(this);
}
});
//A new Element will have the property 'lastChild'
// and that property will return the last child node
// as computed using Node's _lastChild function
// in the scope of Element with the property
// 'childNodes' as inherited from Node itself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment