Last active
April 16, 2016 17:01
-
-
Save gsans/ef8d1196911943a948aa to your computer and use it in GitHub Desktop.
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 Polygon(height, width) { //class constructor | |
this.name = 'Polygon'; | |
this.height = height; | |
this.width = width; | |
} | |
Polygon.prototype = { | |
sayName: function () { //class method | |
console.log('Hi, I am a ' + this.name + '.'); | |
} | |
}; | |
function Square(length) { //class constructor | |
Polygon.call(this, length, length); | |
this.name = 'Square'; | |
} | |
// Square inherits from Polygon | |
Square.prototype = Object.create(Polygon.prototype); | |
// Set the "constructor" property to refer to Square | |
Square.prototype.constructor = Square; | |
// Add getter | |
Square.prototype.getArea = function() { //class method | |
return this.height * this.width; | |
}; | |
var p = new Polygon(5,5); | |
p.sayName(); // Hi, I am a Polygon. | |
var s = new Square(5); | |
s.sayName(); // Hi, I am a Square. | |
console.log(s.getArea()); // 25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment