Skip to content

Instantly share code, notes, and snippets.

@8th-Light-Blog
Created June 27, 2011 20:58
Show Gist options
  • Save 8th-Light-Blog/1049824 to your computer and use it in GitHub Desktop.
Save 8th-Light-Blog/1049824 to your computer and use it in GitHub Desktop.
Blog Title: JavaScriptness.prototype = new Class(); From Classical to Prototypal
Author: Justin Martin
Date: July 7th, 2010
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);
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");
var firstSquare = {
side: 5,
area: function () {
return this.side * this.side;
},
perimeter: function () {
return this.side * 4;
}
};
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;
var squareMaker = function(side) {
return {
getSide: function() {
return side;
},
area: function () {
return side * side;
},
perimeter: function () {
return side * 4;
}
};
};
var anotherSquare = squareMaker(5);
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