Just a question: why static methods?
Opened this issue · 1 comments
amundo commented
Hi, pushbar.js is really nice.
A question: what is the advantage of using static methods as opposed to normal methods in things like static dispatchClose(pushbar)
?
jamsch commented
The purpose of static methods is that you can access generic properties on a class without having to create a class instance. Static methods don't have access to the this
class instance, and therefore are really only there as utility functions and properties.
For example:
class AudioPlayer {
static defaultVolume = 10;
constructor() {
this.volume = AudioPlayer.defaultVolume;
}
}
console.log(AudioPlayer.defaultVolume); // 10
console.log(AudioPlayer.volume); // property doesn't exist on object
const Player = new AudioPlayer();
console.log(Player.volume) // 10
In Pushbar, why we do this is so that we don't have to needlessly bind the class instance to dispatchClose
if it isn't utilising any class instance variables.
const pushbar = new Pushbar();
pushbar.close(); // Closes the current active bar
Pushbar.dispatchClose('pushbar-one'); // Programatically closes a specific pushbar in your DOM