Created
July 3, 2022 19:10
-
-
Save batazo/ccc9c990a90ce6776e123af1b9f081e8 to your computer and use it in GitHub Desktop.
Private instance methods and accessors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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