mrdoob/three.js — pull request #2940 by bhouston (2013-01-17 16:04:40+00:00)
Title: Initial adoption of closures in the math library.  All unit tests pass.  All examples work that I tested.

I've implemented JavaScript closures across the whole math library to remove the need for declaration of public static variables for GC optimization purposes as proposed in issue #2936.

did have to switch to extending the prototypes of the math classes rather than just replacing their prototypes. I had to do this because instances of a type created within closures of that type are created before the prototype is set, thus they were incomplete types. For example, if inside ```THREE.Matrix4.prototype = { ... [in here].... }``` you had a closure that created a ```var m1 = THREE.Matrix4()``` variable, it wouldn't yet have its prototype set because only after it has completed executing all the code in the ```{ ...[in here]... }``` block does it set it to ```Matrix4.prototype```. By extending the prototype, even variables created inside the ```{...[in here]...}``` block eventually get full prototypes before they are used in their methods.

I added THREE.extends to handle extending prototypes, I based it on the code here:

http://stackoverflow.com/a/12317051

I tested about 20 examples at random and they all worked. All unit tests pass as well.

Both ```webgl_performance.html``` and ```webgl_performance_static.html``` perform at the same speed, no slowdowns on my PC.