Revealing Module Pattern Javascript

43 views Asked by At

Which approach is more convenient in an real life project? Can someone please enlighten me? And tell the difference between all of them(efficiency / clean code etc.)

All of them give the same result. Classes are ES6.

2nd approach Constructor pattern combined with Revealing Module Pattern.

3rd is only a constructor pattern / function and the properties are used in an normal function declaration.

I want to make an application which i insert some data from the input form, creating with the classes / constructors and then display it in the UI. Data obviously is always changing( creating multiple objects when i insert new input form)

// 1st approach with classes
const test1 = (() => {
  class TestClass {
    constructor() {
     this.string = 'Class';
    }

  classMethod() {
   console.log(this.string);
  }
 }

  const testClass = new TestClass();

  return {
   testClass
  }
 })();

test1.testClass.classMethod();

// 2nd approach with constructor and constructor protoype methods
const test2 = (() => {
 function TestConstructor() {
  this.string = 'Constructor';
 }

TestConstructor.prototype = {
 constructorMethod() {
  console.log(this.string);
 }
}

const testConstr = new TestConstructor();

 return {
  testConstr
 }
})();

test2.testConstr.constructorMethod();

// 3rd approach with constructor with function declaration
const test3 = (() => {
 function TestConstructor() {
  this.string = 'Constructor with function declaration';
 }

 const testConstr = new TestConstructor();

 function normal() {
  console.log(testConstr.string);
 }

 return {
  normal
 }
})();

test3.normal();
0

There are 0 answers