spite / ThreeJSEditorExtension

Three.js Editor Extension for Google Chrome
MIT License
183 stars 29 forks source link

black magic in a .unshift ? #9

Closed jeromeetienne closed 9 years ago

jeromeetienne commented 9 years ago

you are still mistifying me on this .unshift() :) if i put .push here, all objects are reported as Object3D instead...

what is the black magic ?

    function extractTypes() {

        for( var j in THREE ) {
            if( typeof THREE[ j ] === 'function' ) {
                types.unshift( j );
            }
        }

    }
spite commented 9 years ago

That's precisely the point: basic classes in the THREE object are created in order of inheritance, so -for example- THREE.Mesh inherits from THREE.Object3D. That means that when iterating through all THREE properties that are functions, Mesh is defined after Object3D is defined. If I push in the natural order of constructors to identify the type, it's going to match Object3D before Mesh. Adding constructors in reverse order (using .unshift() ) makes sure I'll always match the most inherited type, instead of the most basic type.

It's far from black magic, but adding a comment to the function would definitely help.

jeromeetienne commented 9 years ago

thanks for the explaination! it was black magick to me :)

have you read your twitter pm recently