Skip to content

Instantly share code, notes, and snippets.

@Winchestro
Created November 30, 2014 03:33
Show Gist options
  • Save Winchestro/c7d8009977c40828fe37 to your computer and use it in GitHub Desktop.
Save Winchestro/c7d8009977c40828fe37 to your computer and use it in GitHub Desktop.
swizzle.js
/*
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