Just treat the ES6 class name the same as you would have treated the constructor name in the ES5 way. They are one and the same.
The ES6 syntax is just syntactic sugar and creates exactly the same underlying prototype, constructor function and objects.
So, in your ES6 example with:
// animal.js
class Animal {
...
}
var a = new Animal();
module.exports = {Animal: Animal};
You can just treat Animal
like the constructor of your object (the same as you would have done in ES5). You can export the constructor. You can call the constructor with new Animal()
. Everything is the same for using it. Only the declaration syntax is different. There's even still an Animal.prototype
that has all your methods on it. The ES6 way really does create the same coding result, just with fancier/nicer syntax.
On the import side, this would then be used like this:
const Animal = require('./animal.js').Animal;
let a = new Animal();
This scheme exports the Animal constructor as the .Animal
property which allows you to export more than one thing from that module.
If you don't need to export more than one thing, you can do this:
// animal.js
class Animal {
...
}
module.exports = Animal;
And, then import it with:
const Animal = require('./animal.js');
let a = new Animal();