3

I'm looking for the best way to write object-oriented (O-O) JavaScript (JS) code for a project I'm working on. My project assumes the latest version of Google Chrome so I have the possibility of using ECMAScript 6's (ES6) features.

While researching O-O JS, I came across the revealing module pattern, which strikes me as a clean way of writing O-O code in a language that does not have proper classes like in Java, for example.

Is there any reason to prefer using ES6 classes?

Please correct me if I'm wrong about this but ES6 classes appear to have the following irredeemable disadvantages:

  1. They make public class variables complicated.
  2. They make private class variables complicated.
  3. They create aesthetically ugly code.
  4. They need to be declared before creating a class instance.

As I hope my example below demonstrates, the revealing module pattern has none of these problems.

var revealingModulePattern = RevealingModulePattern("Revealing Module Pattern");

revealingModulePattern.publicMethodGetName(); // "Revealing Module Pattern" 
revealingModulePattern._privateName; // Undefined  
revealingModulePattern.publicMessage; // "Hello StackOverflow"
revealingModulePattern.publicMessage = "New message";
revealingModulePattern.publicMessage; // "New message"


function RevealingModulePattern(constructorArg)
{
   var _privateName = constructorArg;

   var publicMessage = "Hello StackOverflow";

   function publicMethodGetName() {
      return _privateName;
   }

   return {
      publicMethodGetName: publicMethodGetName,
      publicMessage: publicMessage
   };
}

/***************************************************************/

var _privateName = Symbol();
class ES6Class
{
   constructor(constructorArg) {
      this[_privateName] = constructorArg;
   }

   get name() {
     return this[_privateName];
   }
}

var es6Class = new ES6Class("ES6 Class");

es6Class[_privateName]; // "ES6 Class"
es6Class.name; // "ES6 Class"
es6Class[_privateName] = "New 'private' name";
es6Class.name; // "New 'private' name"

es6Class.publicMessage = "Hello StackOverflow";
es6Class.publicMessage; // "Hello StackOverflow"
es6Class.publicMessage = "New message";
es6Class.publicMessage; // "New message"
Community
  • 1
  • 1
user5508297
  • 805
  • 2
  • 9
  • 24
  • If you don't like an ES6 feature, no one forces you to use it. Also the revealing module pattern solves a different problem than classes do anyway, so there's no reason not to prefer it when it is appropriate. – Bergi Jul 17 '16 at 18:06
  • Your `RevealingModulePattern` function is a factory function, not a module pattern. – Bergi Jul 17 '16 at 18:07
  • Hi @Bergi, thanks for clarifying that. Do you know of any good resources where I can learn more about the difference between factory functions and modules? – user5508297 Jul 17 '16 at 21:11
  • A module is a singleton, the function is called as an IIFE usually. A factory produces multiple objects. – Bergi Jul 17 '16 at 21:40
  • Thanks again @Bergi. Is my style of factory function (FF) OK to use as a replacement for Java-like classes or is there anything else I'm missing? Do I, for example, need to use prototype methods in my FF? – user5508297 Jul 17 '16 at 21:42
  • That all depends on your choice of definition for "Java-like". But if you want to use prototype methods, you should use ES6 `class` syntax for them (you can still put the private variables and privileged methods in the constructor) – Bergi Jul 17 '16 at 21:45
  • @Bergi, in that case it sounds like there may be performance advantages to using ES6 classes rather than FFs. Are all methods in ES6 classes prototype methods? For example, is get name() in my example above, a prototype method? – user5508297 Jul 17 '16 at 21:50
  • "It sounds like" is nothing you should base a decision on :-) If you care enough about performance, do measurements. Yes, all members in a `class` body become prototype members. – Bergi Jul 17 '16 at 21:53
  • OK, good to know. @Bergi, I've got another question which, as someone who is clearly very knowledgeable about this topic, you'll be in a good position to answer: http://stackoverflow.com/questions/38426494/is-there-a-way-to-exploit-the-performance-advantages-of-using-prototype-methods Your help would be highly appreciated! :) – user5508297 Jul 17 '16 at 22:24

0 Answers0