JSFiddle - React, Tailwind, and code Playground
by mjmitche
JavaScript
function Rectangle(w,h) {
this.width = w;
this.height = h;
}
Rectangle.prototype.area = function() {
return (this.width * this.height);
}
Rectangle.prototype.toString = function() {
return "Rectangle of width: " + this.width + " and height: " + this.height;
}
Rectangle.prototype.valueOf = function() {
return this.area();
}
Rectangle.prototype.equals = function(oRect) {
return (this.width == oRect.width && this.height == oRect.height);
}
Rectangle.prototype.compareTo = function(oRect) {
return (this.area() - oRect.area());
}
function DepthRectangle(w,h,d) {
Rectangle.call(this,w,h);
this.depth = d;
}
DepthRectangle.prototype = new Rectangle();
DepthRectangle.prototype.constructor = DepthRectangle;
DepthRectangle.prototype.volume = function() {
return (this.area() * this.depth);
}
DepthRectangle.prototype.toString = function() {
return "DepthRectangle with width: " + this.width + " & height: " + this.height + " & depth: " + this.depth;
}
var myRect = new Rectangle(5,6);
var myDeepRect = new DepthRectangle(7,8,9);
alert(myDeepRect.volume());
alert(myDeepRect.area());
alert(myRect instanceof DepthRectangle);
/*
var myRect2 = new Rectangle(7,8);
var myRect3 = new Rectangle(5,6);
var myRect4 = myRect;
alert(typeof myRect);
alert(myRect instanceof Rectangle);
alert(myRect instanceof Object);
alert(myRect.toString());
alert (myRect.equals(myRect2));
alert (myRect.equals(myRect3));
alert (myRect.equals(myRect4));
*/