I often see classes named like SOMETHING.something - such as in Three.js where one can do

var geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
var mesh = new THREE.Mesh( geometry, material );

but I can't figure how to define a class to do that.

I know I can do this:

class Foo {
    constructor(name) {
    this.name = name;
    }
}


Foo.Bar = function (name) {
    this.name = name;
}

Foo.Bar.prototype.greet = function(){
    console.log(this.name);
} 

var f = new Foo.Bar('gigi');

f.greet()

but it looks like I cannot do this

class Foo {
    constructor(name) {
    this.name = name;
    }
}

class Foo.Bar {
    constructor(name) {
    this.name = name;
    }
}

as I get this:

class Foo.Bar {
         ^
SyntaxError: Unexpected token .

How can that be done?

2 Answers

1
CertainPerformance On Best Solutions

You can assign the new Class to the Bar property of Foo:

class Foo {
  constructor(name) {
    this.name = name;
  }
}

Foo.Bar = class Bar {
  constructor(name) {
    console.log('making a bar');
    this.name = name;
  }
}

const bar = new Foo.Bar();

0
Jonas Wilms On

There are class expressions:

 Foo.Bar = class { /*...*/ };

Can I use a dot in the name of a class?

No. The dot is used in JS to annotate property access, so Foo.Bar will always mean "the Bar property of Foo".