Skip to content

Instantly share code, notes, and snippets.

@kevinpang
Created June 8, 2011 22:40
Show Gist options
  • Save kevinpang/1015628 to your computer and use it in GitHub Desktop.
Save kevinpang/1015628 to your computer and use it in GitHub Desktop.
JavaScript functional inheritance
function Shape(x, y) {
var that = this;
this.x = x;
this.y = y;
this.toString = function() {
return 'Shape at ' + that.x + ', ' + that.y;
};
}
function Circle(x, y, r) {
var that = this;
Shape.call(this, x, y);
this.r = r;
var _baseToString = this.toString;
this.toString = function() {
return 'Circular ' + _baseToString(that) + ' with radius ' + that.r;
};
};
var c = new Circle();
console.log(c.toString()); // "Circular Shape at 1, 2 with radius 3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment