Max Koretskyi
Max Koretskyi

Reputation: 105497

wrap some functionality into function inside a private method

I have a class-constructor with two private methods:

var deleteOption = function(){...};
var removeOption = function(){...};

The first one after doing some operations removes HTML element from the DOM by calling removeOption() method. This is the only place where this method is called. I thought today that maybe it would be a good idea to remove that method from private interface and put inside deleteOption method as function:

var deleteOption = function(){
    ... //do some operations here

    removeOption();
    function removeOption() {
        ...
    }
};

1) Is it a common practice in JavaScript?

2) Is it a good desicions from the standpoint of class architecture?

Upvotes: 0

Views: 125

Answers (1)

SidOfc
SidOfc

Reputation: 4584

If you are to only call it in that function, and not passing an array of elements at all (e.g. no loops) you might as well implement only the functions code rather than the entire function, If removeOption is quite large, comment it as needed as well.

Putting functions inside functions is a commonly used method inside JavaScript but not always usefull. To me it seems you just called another external function that will only execute in one other function, so the best you could do is simply merge those functions.

The profit is that you'll have one function less to call because the code is already in the native function, therefore it will be slightly faster.

Upvotes: 1

Related Questions