Created
February 8, 2019 03:59
-
-
Save dlucidone/28e13f792f8e4c343f31a54e2a6dd617 to your computer and use it in GitHub Desktop.
extend implement
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
// 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