The javascript default field is public (public
), if you need to indicate private, you can use (#
)
class Dog {
#name;
constructor(name) {
this.#name = name;
}
printName() {
// Only private fields can be called inside the class
console.log(`Your name is ${this.#name}`)
}
}
const dog = new Dog("putty")
//console.log(this.#name)
//Private identifiers are not allowed outside class bodies.
dog.printName()
#Static private class
class ClassWithPrivate {
static #privateStaticField;
static #privateStaticFieldWithInitializer = 42;
static #privateStaticMethod() {
// â¦
}
}