If I have a simple component such as the one below, would it better to use static methods within my class or not? I realise this could be down to opinion but what I'm interested in is if there are architectural reasons why one way might have advantages over the other.
Non static methods:
class mainNav {
toggleMainNav() {
const navBtn = document.getElementById('toggle-menu');
const navMenu = document.getElementById('main-nav');
navBtn.addEventListener('click', () => {
navMenu.classList.toggle('open');
});
}
init() {
this.toggleMainNav();
}
}
module.exports = mainNav;
To instantiate I would need to instantiate the class first like so:
const mainNav = require("../js/components/mainNav/mainNav");
//Init nav
const nav = new mainNav;
nav.init();
Alternatively with static methods I could do this:
class mainNav {
static toggleMainNav() {
const navBtn = document.getElementById('toggle-menu');
const navMenu = document.getElementById('main-nav');
navBtn.addEventListener('click', () => {
navMenu.classList.toggle('open');
});
}
static init() {
this.toggleMainNav();
}
}
module.exports = mainNav;
and instantiate:
const mainNav = require("../js/components/mainNav/mainNav");
mainNav.init();
On the face of it using static methods in this case seems easier as I don't have to instantiate an instance of the class first.
Not coming from an OOP programming background, Is there is a reason why doing this would be considered bad practise?