Should I use methods or subclasses? If that makes sense

Little background ...

I have an object called SineMacula in which there are many methods for creating form elements and creating these form elements on a page.

Firstly, when the page loads, a method called setFields() called, which setFields() over all the fields on the page and accordingly sets them ie autocomplete, checkbox, etc.

The code for setFields() as follows:

 /** * Set Fields * This function will set all fields * * The options: * - fields: the fields selector to loop through * * @param object options The field options */ SineMacula.prototype.setFields = function (options){ // Set the defaults for the fields var options = $.extend({ fields: '.field', // Define the default field selector },options); // Loop through the fields and set the events $(options.fields).each(function(){ // Set the field events SineMacula.setBlur($(this)); SineMacula.setFocus($(this)); SineMacula.setToggleLabel($(this)); // If the field is a checkbox then set it if($(this).parent().hasClass('checkbox')){ SineMacula.setCheckbox($(this).parent()); } // If the field is an autocomplete then set it if($(this).parent().hasClass('autocomplete')){ SineMacula.setDropdown($(this).parent(),{source:$(this).attr('data-source')}); } // etc... }); }; 

Most of the code above can be ignored, but I inserted all of this so that you can see exactly what I'm doing.

My question

I have quite a few SineMacula object methods like setCheckbox() , setDropdown() ...

What would I like to know if I should consider these methods as objects in myself?

So should my code look like this:

 if($(this).parent().hasClass('autocomplete')){ new SineMacula.dropdown($(this).parent(),{source:$(this).attr('data-source')}); } 

Note the new keyword before calling dropdown() .

Is this the best working method? Will less memory be used, etc.

+4
source share
3 answers

There is no reason to instantiate an object just to call the constructor, and then throw the object away. Performing work in the constructor, you simply use it as a regular function, but with the overhead of creating an unused object.

(Actually, you don't seem to be using the SineMacula instance for anything other than a namespace for methods.)

+3
source

As a general rule, a new object appears when you need to delegate some responsibility to it. Therefore, if you later do something like sineMaculaInstance.setCheckboxValue(checkbox, true) , it looks like this should be the responsibility of the checkbox. Another way to look at this is to analyze the SineMacula object through the principle of shared responsibility . In short, if you can describe what your object does in one or two lines, you are usually fine. If you need to write a full paragraph to indicate what SineMacula is doing, then it looks like you should reorganize this object into separate, specific responsibilities to other objects.

NTN

+2
source

It seems to me that since you place all your methods in this SineMacula namespace / module, there is no purpose in re-creating another whole new SineMacula object.

If you are not going to add different / specific prototypes / methods that you do not want to attach to the source object, and relate to a specific section or form element on your page.

 var newThing = new SineMacula('doDifferentStuff'); newThing.dropdown = '''do something different'''; 

The whole reason for creating an instance of the class would also be to set the new this as what you are calling it from. And it looks like everything you have is already connected to each other and just uses SineMacula.setBlahblah to call itself.

Hope this doesn't sound too distorted!

+1
source

Source: https://habr.com/ru/post/1443067/


All Articles