object orientation using Object.create
by ozzymcduff
JavaScript
//Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
//superclass method
Shape.prototype.move = function(x, y) {
console.log([this.x,this.y]);
this.x += x;
this.y += y;
console.info("Shape moved.");
console.log([this.x,this.y]);
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); //call super constructor.
}
//subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.shift = function(x){
console.log('shift');
};
//Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();
if (!(rect instanceof Rectangle)){ console.error("instance of rectangle");} //true.
if (!(rect instanceof Shape)){ console.error("instance of shape");} //true.
rect.move(2, 1); //Outputs, "Shape moved."
rect.move(1,2);
rect.shift(1);
var shape= new Shape();
console.log(shape);