Loizos Vasileiou
Loizos Vasileiou

Reputation: 704

Information about constant class in Javascript

Hi all im working on ray tracing algorithm and I want to show different possibilities by shooting lines from different positions in Three.js. I just wanted some information about this line of code below and how to use it or do not use it at all.

const Ray_Techniques = class { 

    MANAGER() {
       return{
          FRT: function(){
              return{
                  //Shoot lines in stage entry
              };
          },
          BRT: function(){
              return{
                  //Shoot lines in stage entry (different algorithm)
              };
          }
       };
    }

    hide_show_repeat_MANAGER(){
        return{
             //hide, show, repeat each stage
        };
    }
};

Is that okay to use ? I'm new to Object Oriented Javascript. I just want to let the user repeat the stage by recalling some of those functions and also hide/show a specific algorithm according to the stage (slider-value). Also is the 'const'class going to affect all these?

Upvotes: 0

Views: 46

Answers (2)

Bergi
Bergi

Reputation: 664444

Is that okay to use?

No, it's not OK to use a class here at all. You only need classes if you have multiple instances with shared methods and different configuration or mutable state. For what you've shown in your question, a simple object will suffice:

const rayTechniques = { 
    manager: {
        FRT() {
            … //Shoot lines in stage entry
        },
        BRT() {
            … //Shoot lines in stage entry (different algorithm)
        }
    },
    hide_show_repeat() {
        … //hide, show, repeat each stage
    }
};

Upvotes: 2

Jtaks
Jtaks

Reputation: 301

The only differences will be that you cannot reassign Ray_Techniques to another value since it is defined as a const. A class defined as class Ray_Techiques {} would allow you to reassign the class name Ray_Techniques to something else.

From MDN:

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.

In my opinion the const declaration detracts from code readability vs named class.

Upvotes: 1

Related Questions