Skip to content

Adopt some ES6 features #6419

@lmcd

Description

@lmcd

ES6 is coming, with browsers and tools now rapidly gaining support. I think THREE.js could benefit enormously from some of the new features brought by ES6.

For fun and to encourage debate surrounding a possible migration of the project, I've created this issue and created some code examples.

Features demonstrated in the below examples:

  • Default parameters
  • Block-scoped let keyword
  • Iterators + For..Of
  • Classes
  • Arrow Functions
  • Generators
  • Modules

Class Example

import Object3D from '../core/Object3D';
import Geometry from '../core/Geometry';
import MeshBasicMaterial from '../materials/MeshBasicMaterial';

class Mesh extends Object3D {
    constructor(
        geometry = new Geometry(),
        material = new MeshBasicMaterial({color: Math.random() * 0xffffff}
    ) {
        super();

        this.geometry = geometry;
        this.material = material;

        this.updateMorphTargets();
    }
}

export default Mesh;

Traversal Generator Example

class Object3D {
    constructor() {
        ...
    }

    traverse(callback) {
        callback(this);

        for (let object of this.children) {
            object.traverse(callback);
        }
    },

    *traversalGenerator() {
        this.traverse(object => yield object);
    }

    ...
}

Generator in use:

for (let object of scene.traversalGenerator()) {
    if (object.name === 'Blah') {
        break;
    }
}

Note: I haven't tested any of this

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions