Created
June 27, 2011 20:58
-
-
Save 8th-Light-Blog/1049824 to your computer and use it in GitHub Desktop.
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
| Blog Title: JavaScriptness.prototype = new Class(); From Classical to Prototypal | |
| Author: Justin Martin | |
| Date: July 7th, 2010 |
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
| function Square (side) { | |
| this.side = side; | |
| }; | |
| Square.prototype.area = function () { | |
| return this.side * this.side; | |
| }; | |
| Square.prototype.perimeter = function () { | |
| return this.side * 4; | |
| }; | |
| var mySquare = new Square (5); |
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
| function ContainerSquare (side, contents) { | |
| this.superclass(side); | |
| this.contents = contents; | |
| }; | |
| ContainerSquare.prototype = new Square(); | |
| ContainerSquare.prototype.superclass = Square; | |
| ContainerSquare.prototype.constructor = ContainerSquare; | |
| ContainerSquare.prototype.getContents = function () { | |
| return this.contents; | |
| }; | |
| var myContainer = new ContainerSquare(6, "X"); |
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
| var firstSquare = { | |
| side: 5, | |
| area: function () { | |
| return this.side * this.side; | |
| }, | |
| perimeter: function () { | |
| return this.side * 4; | |
| } | |
| }; |
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
| if (typeof Object.beget !== 'function') { | |
| Object.beget = function (o) { | |
| var F = function () {}; | |
| F.prototype = o; | |
| return new F(); | |
| } | |
| } | |
| var secondSquare = Object.beget(firstSquare); | |
| secondSquare.side = 6; |
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
| var squareMaker = function(side) { | |
| return { | |
| getSide: function() { | |
| return side; | |
| }, | |
| area: function () { | |
| return side * side; | |
| }, | |
| perimeter: function () { | |
| return side * 4; | |
| } | |
| }; | |
| }; | |
| var anotherSquare = squareMaker(5); |
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
| var containerMaker = function(side, contents) { | |
| var container = squareMaker(side); | |
| container.getContents = function () { | |
| return contents; | |
| }; | |
| return container; | |
| }; | |
| var anotherContainer = containerMaker(6, "O"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment