Skip to content

Instantly share code, notes, and snippets.

@gsans
Last active April 16, 2016 17:01
Show Gist options
  • Save gsans/ef8d1196911943a948aa to your computer and use it in GitHub Desktop.
Save gsans/ef8d1196911943a948aa to your computer and use it in GitHub Desktop.
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