-
-
Save ksato9700/1074697 to your computer and use it in GitHub Desktop.
JavaScript prototypal 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) { | |
this.x = x; | |
this.y = y; | |
} | |
Shape.prototype.toString = function() { | |
return 'Shape at ' + this.x + ', ' + this.y; | |
}; | |
function Circle(x, y, r) { | |
Shape.call(this, x, y); // invoke the base class's constructor function to take co-ords | |
this.r = r; | |
} | |
Circle.prototype = new Shape(); | |
Circle.prototype.toString = function() { | |
return 'Circular ' + Shape.prototype.toString.call(this) + ' with radius ' + this.r; | |
} | |
var c = new Circle(1, 2, 3); | |
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