Advertisement
stanevplamen

ES6 private props

Sep 23rd, 2022
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.30 KB | Software | 0 0
  1. class Person {
  2.   #age
  3.  
  4.   constructor(name) {
  5.     this.name = name; // this is public
  6.     this.#age = 20; // this is private
  7.   }
  8.  
  9.   greet() {
  10.     // here we can access both name and age
  11.     console.log(`name: ${this.name}, age: ${this.#age}`);
  12.   }
  13. }
  14.  
  15. let joe = new Person('Joe');
  16. joe.greet();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement