Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Object.prototype.extends = function (properties) {
- function f() { } ;
- var prop;
- f.prototype = Object.create(this);
- for (prop in properties) {
- f.prototype[prop] = properties[prop];
- };
- f.prototype._super = this;
- return new f();
- }
- var Prototypal = (function() {
- var shape = {
- init: function(x, y, color) {
- this._x = x;
- this._y = y;
- this._color = color;
- return this;
- },
- toString: function() {
- return 'X: ' + this._x + ', Y: ' + this._y;
- }
- }
- var circle = shape.extends({
- init: function(x, y, color, r) {
- this._super.init.call(this, x, y, color);
- this._r = r;
- return this;
- },
- toString: function() {
- return this._super.toString.call(this) + ', R: ' + this._r;
- }
- });
- var rect = shape.extends({
- init: function(x, y, width, height, color) {
- this._super.init.call(this, x, y, color);
- this._width = width;
- this._height = height;
- return this;
- },
- toString: function() {
- return this._super.toString.call(this) + ', Width: ' + this._width + ', Height: ' + this._height;
- }
- });
- var triangle = shape.extends({
- init: function(x, y, x1, y1, x2, y2, color) {
- this._super.init.call(this, x, y, color);
- this._x1 = x1;
- this._y1 = y1;
- this._x2 = x2;
- this._y2 = y2;
- return this;
- },
- toString: function() {
- return this._super.toString.call(this) + ', X1: ' + this._x1 + ', Y1: ' + this._y1 +
- ', X2: ' + this._x2 + ', Y2: ' + this._y2;
- }
- });
- var line = shape.extends({
- init: function(x, y, x1, y1, color) {
- this._super.init.call(this, x, y, color);
- this._x1 = x1;
- this._y1 = y1;
- return this;
- },
- toString: function() {
- return this._super.toString.call(this) + ', X1: ' + this._x1 + ', Y1: ' + this._y1;
- }
- });
- var segment = shape.extends({
- init: function(x, y, x1, y1, color) {
- this._super.init.call(this, x, y, color);
- this._x1 = x1;
- this._y1 = y1;
- return this;
- },
- toString: function() {
- return this._super.toString.call(this) + ', X1: ' + this._x1 + ', Y1: ' + this._y1;
- }
- });
- return {
- Shape: shape,
- Circle: circle,
- Rect: rect,
- Triangle: triangle,
- Line: line,
- Segment: segment
- }
- }());
- var shape = Object.create(Prototypal.Shape).init(1, 2, "660066");
- console.log(shape.toString());
- var circle = Object.create(Prototypal.Circle).init(1, 2, "662266", 5);
- console.log(circle.toString());
- var rect = Object.create(Prototypal.Rect).init(1, 2, 4, 4, "662266");
- console.log(rect.toString());
- var triangle = Object.create(Prototypal.Triangle).init(1, 2, 4, 4, 5, 5, "662266");
- console.log(triangle.toString());
- var line = Object.create(Prototypal.Line).init(1, 2, 4, 4, "662266");
- console.log(line.toString());
- var segment = Object.create(Prototypal.Segment).init(4, 4, 2, 2, "662266");
- console.log(segment.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement