Created
June 8, 2011 22:40
-
-
Save kevinpang/1015628 to your computer and use it in GitHub Desktop.
JavaScript functional inheritance
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 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
Taken from http://stackoverflow.com/questions/1595611/how-to-properly-create-a-custom-object-in-javascript/1598077#1598077