Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Just a question: why static methods? #7

Open
amundo opened this issue Aug 9, 2018 · 1 comment
Open

Just a question: why static methods? #7

amundo opened this issue Aug 9, 2018 · 1 comment

Comments

@amundo
Copy link

amundo commented Aug 9, 2018

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
Copy link
Contributor

jamsch commented Sep 12, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants