Advertisement
Guest User

Untitled

a guest
Mar 17th, 2015
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Object.prototype.extends = function (properties) {
  2.     function f() { }    ;
  3.     var prop;
  4.     f.prototype = Object.create(this);
  5.     for (prop in properties) {
  6.         f.prototype[prop] = properties[prop];
  7.     };
  8.     f.prototype._super = this;
  9.     return new f();
  10. }
  11.  
  12. var Prototypal = (function() {
  13.     var shape = {
  14.         init: function(x, y, color) {
  15.             this._x = x;
  16.             this._y = y;
  17.             this._color = color;
  18.             return this;
  19.         },
  20.         toString: function() {
  21.             return 'X: ' + this._x + ', Y: ' + this._y;
  22.         }
  23.     }
  24.  
  25.     var circle = shape.extends({
  26.         init: function(x, y, color, r) {
  27.             this._super.init.call(this, x, y, color);
  28.             this._r = r;
  29.             return this;
  30.         },
  31.         toString: function() {
  32.             return this._super.toString.call(this) + ', R: ' + this._r;
  33.         }
  34.     });
  35.  
  36.     var rect = shape.extends({
  37.         init: function(x, y, width, height, color) {
  38.             this._super.init.call(this, x, y, color);
  39.             this._width = width;
  40.             this._height = height;
  41.             return this;
  42.         },
  43.         toString: function() {
  44.             return this._super.toString.call(this) + ', Width: ' + this._width + ', Height: ' + this._height;
  45.         }
  46.     });
  47.  
  48.     var triangle = shape.extends({
  49.         init: function(x, y, x1, y1, x2, y2, color) {
  50.             this._super.init.call(this, x, y, color);
  51.             this._x1 = x1;
  52.             this._y1 = y1;
  53.             this._x2 = x2;
  54.             this._y2 = y2;
  55.             return this;
  56.         },
  57.         toString: function() {
  58.             return this._super.toString.call(this) + ', X1: ' + this._x1 + ', Y1: ' + this._y1 +
  59.                 ', X2: ' + this._x2 + ', Y2: ' + this._y2;
  60.         }
  61.     });
  62.  
  63.     var line = shape.extends({
  64.         init: function(x, y, x1, y1, color) {
  65.             this._super.init.call(this, x, y, color);
  66.             this._x1 = x1;
  67.             this._y1 = y1;
  68.             return this;
  69.         },
  70.         toString: function() {
  71.             return this._super.toString.call(this) + ', X1: ' + this._x1 + ', Y1: ' + this._y1;
  72.         }
  73.     });
  74.  
  75.     var segment = shape.extends({
  76.         init: function(x, y, x1, y1, color) {
  77.             this._super.init.call(this, x, y, color);
  78.             this._x1 = x1;
  79.             this._y1 = y1;
  80.             return this;
  81.         },
  82.         toString: function() {
  83.             return this._super.toString.call(this) + ', X1: ' + this._x1 + ', Y1: ' + this._y1;
  84.         }
  85.     });
  86.  
  87.     return {
  88.         Shape: shape,
  89.         Circle: circle,
  90.         Rect: rect,
  91.         Triangle: triangle,
  92.         Line: line,
  93.         Segment: segment
  94.     }
  95. }());
  96.  
  97. var shape = Object.create(Prototypal.Shape).init(1, 2, "660066");
  98. console.log(shape.toString());
  99.  
  100. var circle = Object.create(Prototypal.Circle).init(1, 2, "662266", 5);
  101. console.log(circle.toString());
  102.  
  103. var rect = Object.create(Prototypal.Rect).init(1, 2, 4, 4, "662266");
  104. console.log(rect.toString());
  105.  
  106. var triangle = Object.create(Prototypal.Triangle).init(1, 2, 4, 4, 5, 5, "662266");
  107. console.log(triangle.toString());
  108.  
  109. var line = Object.create(Prototypal.Line).init(1, 2, 4, 4, "662266");
  110. console.log(line.toString());
  111.  
  112. var segment = Object.create(Prototypal.Segment).init(4, 4, 2, 2, "662266");
  113. console.log(segment.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement