Skip to content

Instantly share code, notes, and snippets.

@bga
Created October 28, 2010 18:58
Show Gist options
  • Save bga/652072 to your computer and use it in GitHub Desktop.
Save bga/652072 to your computer and use it in GitHub Desktop.
(function($G)
{
var defaultThis = (function(){ return this; })();
var Vector = function(x, y, z)
{
// cast
if(this === defaultThis)
{
return Vector.prototype._calc(x);
}
// new operator
this.x = x;
this.y = y;
this.z = z;
this.id_ = this.instances.uuid++;
this.instances[this.id_] = this;
};
Vector.prototype.instances = {uuid: 0};
Vector.prototype.valueOf = function()
{
return '{' + this.id_ + '}';
};
Vector.prototype.toString = function()
{
return '<'.concat(this.x, ', ', this.y, ', ', this.z, '>');
};
Vector.prototype._calc = function(expr)
{
var ids = expr.slice(1, -1).split('}{');
var x = 0, y = 0, z = 0;
var instances = Vector.prototype.instances;
//console.log(ids);
var i = ids.length; while(i--)
{
var v = instances[ids[i]];
x += v.x;
y += v.y;
z += v.z;
}
return new Vector(x, y, z);
};
$G.Vector = Vector;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment