0

Is there more elegant way to define functions on prototypes than in the code below? Ideally you would define everything inside Vector function.

var Vector = function (x, y) {
    this.x = x;
    this.y = y;
};

Vector.prototype.plus = function (vector) {
    return new Vector (this.x + vector.x, this.y + this.y);
};

Vector.prototype.minus = function (vector) {
    return new Vector (this.x - vector.x, this.y - vector.y);
};

Object.defineProperty(Vector.prototype, "length", {
    get: function () {
        return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
    }
});

var vt = new Vector(3, 4);
var vt2 = new Vector(5, 6);
Karolis Ramanauskas
  • 1,045
  • 1
  • 9
  • 14

0 Answers0