I'm trying to migrate my code from ES5 modules to ES6 classes. My current code (the module, not the class), looks as follows:
var appleModule = (function() {
var yummy = true;
var publicEat = function() { }
var privateEat = function() { }
return { "eat": publicEat }
})();
The above allows me to create private variables and methods that I don't expose to other Javascript objects/scopes in my environment. I like that. I figure in ES6 we have classes now so something like this becomes the refactored code:
exports class appleModule {
constructor {
this.yummy = true;
}
publicEat() {
}
privateEat() {
// How to make private?
}
}
However, there isn't a good way to make private variables and functions with this new class-based syntax. Is the new recommendation to use exports
with the previous module pattern (closure returning object of public members)? If not, how can I simulate private scope like I did with the first example (hopefully without underscores)?