Advertisement
dim4o

Geometry-Prototype-Inheritance

Mar 20th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var shapes = (function () {
  2.  
  3.     Object.prototype.inherits = function (properties) {
  4.         function F() {}
  5.         var prop;
  6.         F.prototype = Object.create(this);
  7.         for (prop in properties) {
  8.             F.prototype[prop] = properties[prop];
  9.         }
  10.  
  11.         F.prototype._super = this;
  12.         return new F();
  13.     };
  14.  
  15.     // Validations
  16.     function isNumber(num) {
  17.         return !isNaN(parseFloat(num)) && isFinite(num);
  18.     }
  19.  
  20.     function isValidCoordinate(coordinate) {
  21.         if (!isNumber(coordinate)) {
  22.             throw new Error("Invalid point coordinate!");
  23.         }
  24.     }
  25.  
  26.     function isValidDimension(dim) {
  27.  
  28.         if (!isNumber(dim) || dim < 0) {
  29.             throw new Error("Invalid dimension!");
  30.         }
  31.     }
  32.  
  33.     function isValidColor(color) {
  34.         var regEx = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i;
  35.         if (!regEx.test(color)) {
  36.             throw  new Error("Invalid color hexadecimal format!");
  37.         }
  38.     }
  39.  
  40.     var point = {
  41.         init: function init(x, y) {
  42.             this.setX(x);
  43.             this.setY(y);
  44.             return this;
  45.         },
  46.  
  47.         getX: function () {
  48.             return this._x;
  49.         },
  50.  
  51.         getY: function () {
  52.             return this._y;
  53.         },
  54.  
  55.         setX: function (x) {
  56.             isValidCoordinate(x);
  57.             this._x = x;
  58.         },
  59.  
  60.         setY: function (y) {
  61.             isValidCoordinate(y);
  62.             this._y = y;
  63.         },
  64.  
  65.         toString: function introduce() {
  66.             return "(" + this.getX() + ", " + this.getY() +")";
  67.         }
  68.     };
  69.  
  70.     var shape = point.inherits({
  71.         init: function init(firstPo, color) {
  72.             this._super.init(firstPo._x, firstPo._y);
  73.             this.setColor(color);
  74.             return this;
  75.         },
  76.  
  77.         getColor: function () {
  78.             return this._color;
  79.         },
  80.  
  81.         setColor: function (color) {
  82.             isValidColor(color);
  83.             this._color = color;
  84.         },
  85.  
  86.         toString: function introduce() {
  87.             return "color = " + this.getColor()
  88.                 + ", first point: " + this._super.toString.call(this);
  89.         }
  90.     });
  91.  
  92.     var line = shape.inherits({
  93.         init: function init(firstPoint, secondPoint, color) {
  94.             this._super.init(firstPoint, color);
  95.             this.setSecondPoint(secondPoint);
  96.             return this;
  97.         },
  98.  
  99.         getSecondPoint: function () {
  100.             return this._secondPoint;
  101.         },
  102.  
  103.         setSecondPoint: function (secondPoint) {
  104.             this._secondPoint = secondPoint;
  105.         },
  106.  
  107.         toString: function introduce() {
  108.             return this._super.toString()
  109.                 + ", second point: " + this.getSecondPoint();
  110.         }
  111.     });
  112.  
  113.     var segment = line.inherits({
  114.         init: function init(firstPoint, secondPoint, color) {
  115.             this._super.init(firstPoint, secondPoint, color);
  116.             return this;
  117.         },
  118.         toString: function introduce() {
  119.             return this._super.toString();
  120.         }
  121.     });
  122.  
  123.     var triangle = segment.inherits({
  124.         init: function init(firstPoint, secondPoint, thirdPoint, color) {
  125.             this._super.init(firstPoint, secondPoint, color);
  126.             this.setThirdPoint(thirdPoint);
  127.             return this;
  128.         },
  129.  
  130.         getThirdPoint: function () {
  131.             return this._thirdPoint;
  132.         },
  133.  
  134.         setThirdPoint: function (thirdPoint) {
  135.             this._thirdPoint = thirdPoint;
  136.         },
  137.  
  138.         toString: function introduce() {
  139.             return this._super.toString()
  140.                 + ", third point: " + this.getThirdPoint();
  141.         }
  142.     });
  143.  
  144.     var circle = shape.inherits({
  145.        init: function (centerPoint, radius, color) {
  146.            this._super.init(centerPoint, color);
  147.            this._radius = radius;
  148.            return this;
  149.        },
  150.  
  151.         getRadius: function () {
  152.             return this._radius;
  153.         },
  154.  
  155.         setRadius: function (radius) {
  156.             this._radius = radius;
  157.         },
  158.  
  159.        toString: function introduce() {
  160.            var result = this._super.toString()
  161.                + ", radius: " + this._radius;
  162.            return result.replace("first", "center");
  163.        }
  164.     });
  165.  
  166.     var rectangle = shape.inherits({
  167.         init: function init(startPoint, width, height, color) {
  168.             this._super.init(startPoint, color);
  169.             this.setWidth(width);
  170.             this.setHeight(height);
  171.             return this;
  172.         },
  173.  
  174.         getWidth: function () {
  175.             return this._width;
  176.         },
  177.         getHeight: function () {
  178.             return this._height;
  179.         },
  180.  
  181.         setWidth: function (width) {
  182.             isValidDimension(width);
  183.             this._width = width;
  184.         },
  185.         setHeight: function (height) {
  186.             isValidDimension(height);
  187.             this._height = height;
  188.         },
  189.  
  190.         toString: function toString() {
  191.             return this._super.toString()
  192.             + ", width: " + this.getWidth() + ", "
  193.             + "height: " + this.getHeight();
  194.         }
  195.     });
  196.     return{
  197.         point: point,
  198.         line: line,
  199.         triangle: triangle,
  200.         rectangle: rectangle,
  201.         circle: circle,
  202.         segment: segment
  203.     }
  204. }());
  205.  
  206. var p1 = Object.create(shapes.point).init(3, "4");
  207. var p2 = Object.create(shapes.point).init(8, 11);
  208. var p3 = Object.create(shapes.point).init(5, 7);
  209.  
  210. var li = Object.create(shapes.line).init(p1, p2, "#aa3455");
  211. console.log("Line: " + li.toString());
  212.  
  213. var seg = Object.create(shapes.segment).init(p1, p2, "#aaaaaa");
  214. console.log("Segment: " + seg.toString());
  215.  
  216. var tri = Object.create(shapes.triangle).init(p1, p2, p3, "#111111");
  217. console.log("Triangle: " + tri.toString());
  218.  
  219. var rec = Object.create(shapes.rectangle).init(p1, 5, 6, "#333");
  220. console.log("Rectangle: " + rec.toString());
  221.  
  222. var cir = Object.create(shapes.circle).init(p1, 12, "#778899");
  223. console.log("Circle: " + cir.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement