Answers for "es6 class private property"

1

Private properties in JavaScript ES6 classes

class Something {
  #property;

  constructor(){
    this.#property = "test";
  }

  #privateMethod() {
    return 'hello world';
  }

  getPrivateMessage() {
      return this.#property;
  }
}

const instance = new Something();
console.log(instance.property); //=> undefined
console.log(instance.privateMethod); //=> undefined
console.log(instance.getPrivateMessage()); //=> test
Posted by: Guest on September-08-2021
0

js es6 class private method

class MyClass
{
  	// Prepend hash to make it private. Browser support at:
  	//	https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields#browser_compatibility
    // Dynamic access (ie: this.#[fieldName]) is disallowed by design.
  	#myPrivateMethod() {
    	// do private things
	}
}
Posted by: Guest on December-20-2021

Code answers related to "es6 class private property"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language