Skip to content

Instantly share code, notes, and snippets.

@dlucidone
Created February 8, 2019 03:59
Show Gist options
  • Save dlucidone/28e13f792f8e4c343f31a54e2a6dd617 to your computer and use it in GitHub Desktop.
Save dlucidone/28e13f792f8e4c343f31a54e2a6dd617 to your computer and use it in GitHub Desktop.
extend implement
// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();
console.log('Is rect an instance of Rectangle?',
rect instanceof Rectangle); // true
console.log('Is rect an instance of Shape?',
rect instanceof Shape); // true
rect.move(1, 1); // Outputs, 'Shape moved.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment