Venciity

OOP in JavaScript Shapes

Nov 2nd, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Object.prototype.extends = function (parent) {
  2.     // for old browsers version
  3.     if (!Object.create) {
  4.         Object.prototype.create = function (proto) {
  5.             function F() { };
  6.             F.prototype = proto;
  7.             return new F;
  8.         };
  9.     };
  10.  
  11.     this.prototype = Object.create(parent.prototype);
  12.     this.prototype.constructor = this;
  13. };
  14.  
  15. // ----------Shape----------
  16. var Shape = (function () {
  17.  
  18.     var Shape = function (x, y, color) {
  19.         this._x = x;
  20.         this._y = y;
  21.         this._color = color;
  22.     };
  23.  
  24.     Shape.prototype = {
  25.         toString: function () {
  26.             var shapeInfo = "X:" + this._x + ", Y:" + this._y + ", Color in hexadecimal format:" + this._color;
  27.             return shapeInfo;
  28.         }
  29.     };
  30.  
  31.     return Shape;
  32.  
  33. }());
  34.  
  35. // ----------Rectangle----------
  36. var Rectangle = (function () {
  37.  
  38.     var Rectangle = function (x, y, color, width, height) {
  39.         Shape.call(this, x, y, color);
  40.         this._width = width;
  41.         this._height = height;
  42.     };
  43.  
  44.     Rectangle.extends(Shape);
  45.  
  46.     Rectangle.prototype.toString = function () {
  47.         return Shape.prototype.toString.call(this) +
  48.             ", Width:" + this._width +
  49.             ", Height:" + this._height;
  50.     };
  51.  
  52.     return Rectangle;
  53. }());
  54.  
  55. // ----------Triangle----------
  56. var Triangle = (function () {
  57.  
  58.     var Triangle = function (x, y, color, x2, y2, x3, y3) {
  59.         Shape.call(this, x, y, color);
  60.         this._x2 = x2;
  61.         this._y2 = y2;
  62.         this._x3 = x3;
  63.         this._y3 = y3;
  64.     };
  65.  
  66.     Triangle.extends(Shape);
  67.  
  68.     Triangle.prototype.toString = function () {
  69.         return Shape.prototype.toString.call(this) +
  70.             ", x2:" + this._x2 +
  71.             ", y2:" + this._y2 +
  72.             ", x3:" + this._x3 +
  73.             ", y3:" + this._y3;
  74.     };
  75.  
  76.     return Triangle;
  77. }());
  78.  
  79. // ----------Segment----------
  80. var Segment = (function () {
  81.  
  82.     var Segment = function (x, y, color, x2, y2) {
  83.         Shape.call(this, x, y, color);
  84.         this._x2 = x2;
  85.         this._y2 = y2;
  86.     };
  87.  
  88.     Segment.extends(Shape);
  89.  
  90.     Segment.prototype.toString = function () {
  91.         return Shape.prototype.toString.call(this) +
  92.             ", x2:" + this._x2 +
  93.             ", y2:" + this._y2;
  94.     };
  95.  
  96.     return Segment;
  97. }());
  98.  
  99. // ----------Point----------
  100. var Point = (function () {
  101.  
  102.     var Point = function (x, y, color) {
  103.         Shape.call(this, x, y, color);
  104.     };
  105.  
  106.     Point.extends(Shape);
  107.  
  108.     Point.prototype.toString = function () {
  109.         return Shape.prototype.toString.call(this);
  110.     };
  111.  
  112.     return Point;
  113. }());
  114.  
  115. //var rectangle = new Rectangle(5, 4, "#FFF", 10, 12);
  116. //var triangle = new Triangle(6, 4, "#F54", 7, 17, 11, 25);
  117. //var segment = new Segment(2, 3, "#001", 5, 16);
  118. //var point = new Point(4, 5, "#ff9");
  119.  
  120. //console.log(rectangle.toString());
  121. //console.log(triangle.toString());
  122. //console.log(segment.toString());
  123. //console.log(point.toString());
Advertisement
Add Comment
Please, Sign In to add comment