Skip to content

Instantly share code, notes, and snippets.

@batazo
Created July 3, 2022 19:10
Show Gist options
  • Save batazo/ccc9c990a90ce6776e123af1b9f081e8 to your computer and use it in GitHub Desktop.
Save batazo/ccc9c990a90ce6776e123af1b9f081e8 to your computer and use it in GitHub Desktop.
Private instance methods and accessors
class Banner extends HTMLElement {
// Private variable that cannot be reached directly from outside, but can be modified by the methods inside:
#slogan = "Hello there!"
#counter = 0
// private getters and setters (accessors):
get #slogan() {return #slogan.toUpperCase()}
set #slogan(text) {this.#slogan = text.trim()}
get #counter() {return #counter}
set #counter(value) {this.#counter = value}
constructor() {
super();
this.onmouseover = this.#mouseover.bind(this);
}
// private method:
#mouseover() {
this.#counter = this.#counter++;
this.#slogan = `Hello there! You've been here ${this.#counter} times.`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment