pushbar.js icon indicating copy to clipboard operation
pushbar.js copied to clipboard

Just a question: why static methods?

Open amundo opened this issue 7 years ago • 1 comments

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)?

amundo avatar Aug 09 '18 15:08 amundo

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

jamsch avatar Sep 12 '18 04:09 jamsch