Skip to content

Instantly share code, notes, and snippets.

@matthewblewitt
Created May 3, 2018 13:13
Show Gist options
  • Save matthewblewitt/6a36cee3e560dc33e9edf09645709f99 to your computer and use it in GitHub Desktop.
Save matthewblewitt/6a36cee3e560dc33e9edf09645709f99 to your computer and use it in GitHub Desktop.
// Shape - superclass
// x,y: location of shape's bounding rectangle
function Shape(x, y) {
  this.x = x;
  this.y = y;
}

// Superclass method
Shape.prototype.move = function(x, y) {
  this.x += x;
  this.y += y;
}

// Circle - subclass
function Circle(x, y, r) {
  // Call constructor of superclass to initialize superclass-derived members.
  Shape.call(this, x, y);

  // Initialize subclass's own members
  this.r = r;
}

// Circle derives from Shape
Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.constructor = Circle;

// Subclass methods. Add them after Circle.prototype is created with
// Object.create
Circle.prototype.area = function() {
  return this.r * 2 * Math.PI;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment