Skip to content

Instantly share code, notes, and snippets.

@azproduction
Created July 18, 2012 08:31
Show Gist options
  • Save azproduction/3135050 to your computer and use it in GitHub Desktop.
Save azproduction/3135050 to your computer and use it in GitHub Desktop.
Point
var Point = function (x, y) {
if (arguments.length === 1) {
return new Point.fromString(x);
}
this.x = x;
this.y = y;
};
Point.fromString = function (x) {
this.x = 0;
this.y = 0;
x = this.parse(x);
x.forEach(function (values) {
if (typeof values === "object") {
this.x += values.x;
this.y += values.y;
}
}.bind(this));
};
Point.fromString.prototype =
Point.prototype = {
parse: function (value) {
return JSON.parse('[' + value + '0]');
},
valueOf: function () {
return JSON.stringify(this) + ',';
},
toString: function () {
return '{' + this.x + ',' + this.y + '}';
}
};
console.log(new Point( new Point(10000, 20000) + new Point(30000, 50000) ).toString()); // {40000,70000}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment