-
-
Save videlais/7fcdcf6ce1f693ef3290 to your computer and use it in GitHub Desktop.
JS Lesson 10
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(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