Created
November 30, 2014 03:33
-
-
Save Winchestro/c7d8009977c40828fe37 to your computer and use it in GitHub Desktop.
swizzle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
assumes you put your constructors for vec2,vec3,etc on your prototype or in global scope | |
because the Function constructor will move the scope of the getters & setters to the global scope | |
if you don't care about a bunch of closures you could also just use normal functions. | |
*/ | |
["x","y","z"].map(function(ex,x,a){ | |
Object.defineProperty(vec3.prototype,a[x],{ | |
get:new Function("return this["+x+"];"), | |
set:new Function("v","this["+x+"] = v;") | |
}); | |
return a.map(function(ey,y){ | |
Object.defineProperty(vec3.prototype,a[x]+a[y],{ | |
get:new Function("return new this.vec2(this["+x+"],this["+y+"]);"), | |
set:new Function("v","this["+x+"]=v[0];this["+y+"]=v[1];") | |
}); | |
return a.map(function(ez,z){ | |
Object.defineProperty(vec3.prototype,a[x]+a[y]+a[z],{ | |
get:new Function("return new this.vec3(this["+x+"],this["+y+"],this["+z+"]);"), | |
set:new Function("v","this["+x+"]=v[0];this["+y+"]=v[1];this["+z+"]=v[2];") | |
}) | |
}) | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment