Friday, April 16, 2021

ES2019 Added Class Private Access Modifier

ES2016 had introduced class without the access modified. Refer to my below article. 

https://scalaoncloud.blogspot.com/2016/12/javascript-evolution-esmascript-6.html


ES2019 has new Class Access Modified. You can add a private modified with # infont. For example:

class SponsorWidget {
  constructor(namedescriptionurl) {
    this.name = name;
    this.description = description;
    this.url = url;
  }

  // Private method
  #buildURL(url) {
    console.log(url);
  }

  render() {
    // Calling private method
    this.#buildURL(this.url);
  }
}

// Here is how we involve the class
let sponsorWidget = new SponsorWidget(
  "Google",
  "Search",
  "https://www.google.com"
);
sponsorWidget.render();

// Error throw.
sponsorWidget.#buildURL("https://www.hotmail.com");

No comments:

Post a Comment