Advertisement
velio84

GeometryPrototypeModel

Mar 12th, 2015
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Shapes = (function() {
  2.     Object.prototype.extends = function (properties) {
  3.         function f() {};
  4.         var prop;
  5.         f.prototype = Object.create(this);
  6.         for (prop in properties) {
  7.             f.prototype[prop] = properties[prop];
  8.         };
  9.         f.prototype._super = this;
  10.         return new f();
  11.     }
  12.  
  13.     var shape = {
  14.         init: function init(x1, y1, color) {
  15.             this.x1 = x1;
  16.             this.y1 = y1;
  17.             this.color = color;
  18.             return this;
  19.         },
  20.  
  21.         toString: function toString() {
  22.             var output = "A[" + this.x1 + ", " + this.y1 + "], " + "color = " + this.color;
  23.  
  24.             return output;
  25.         }
  26.     };
  27.  
  28.     var circle = shape.extends({
  29.         init: function init(x1, y1, r, color) {
  30.             this._super.init(x1, y1, color);
  31.             this.radius = r;
  32.             return this;
  33.         },
  34.  
  35.         toString: function toString() {
  36.             var output = "O[" + this.x1 + ", " + this.y1 + "], radius = " + this.radius + ", color = " + this.color;
  37.  
  38.             return output;
  39.         }
  40.     });
  41.  
  42.     var rectangle = shape.extends({
  43.         init: function init(x1, y1, width, height, color) {
  44.             this._super.init(x1, y1, color);
  45.             this.width = width;
  46.             this.height = height;
  47.             return this;
  48.         },
  49.  
  50.         toString: function toString() {
  51.             var output = "A[" + this.x1 + ", " + this.y1 + "], width = " +
  52.                 this.width + ", height = " + this.height + ", color = " + this.color;
  53.  
  54.             return output;
  55.         }
  56.     });
  57.  
  58.     var triangle = shape.extends({
  59.         init: function init(x1, y1, x2, y2, x3, y3, color) {
  60.             this._super.init(x1, y1, color);
  61.             this.x2 = x2;
  62.             this.y2 = y2;
  63.             this.x3 = x3;
  64.             this.y3 = y3;
  65.             return this;
  66.         },
  67.  
  68.         toString: function toString() {
  69.             var output = "A[" + this.x1 + ", " + this.y1 + "], " +
  70.                 "B[" + this.x2 + ", " + this.y2 + "], " +
  71.                 "C[" + this.x3 + ", " + this.y3 + "], " +
  72.                 "color = " + this.color;
  73.  
  74.             return output;
  75.         }
  76.     });
  77.  
  78.     var line = shape.extends({
  79.         init: function init(x1, y1, x2, y2, color) {
  80.             this._super.init(x1, y1, color);
  81.             this.x2 = x2;
  82.             this.y2 = y2;
  83.             return this;
  84.         },
  85.  
  86.         toString: function toString() {
  87.             var output ="A[" + this.x1 + ", " + this.y1 + "], " +
  88.                 "B[" + this.x2 + ", " + this.y2 + "], " +
  89.                 "color = " + this.color;
  90.  
  91.             return output;
  92.         }
  93.     });
  94.  
  95.     var segment = line.extends({
  96.         init: function init(x1, y1, x2, y2, color) {
  97.             this._super.init(x1, y1, x2, y2, color);
  98.             return this;
  99.         },
  100.  
  101.         toString: function toString() {
  102.             return this._super.toString();
  103.         }
  104.     });
  105.  
  106.     return {
  107.         shape: shape,
  108.         circle: circle,
  109.         rectangle: rectangle,
  110.         triangle: triangle,
  111.         line: line,
  112.         segment: segment
  113.     }
  114. }());
  115.  
  116. var l = Object.create(Shapes.line).init(2, 2, 6, 6, "EFEFEF");
  117. console.log(l.toString());
  118. console.log();
  119.  
  120. var se = Object.create(Shapes.segment).init(7, 7, 12, 12, "123456");
  121. console.log(se.toString());
  122. console.log();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement