Starting with ES13/ES2022, you can mark any class method as private by prefixing the method name with #
(hash symbol).
For example:
// ES13+
class Foo {
#privateMethod() {
return 'foobar';
}
}
const foo = new Foo();
// SyntaxError
console.log(foo.#privateMethod);
Similarly, you can make getter, setter, generator, async, or async generator methods private as well:
// ES13+ class MyClass { #privateMethod() {} * #privateGeneratorMethod() {} async #privateAsyncMethod() {} async * #privateAsyncGeneratorMethod() {} get #privateGetter() {} set #privateSetter(value) {} }
In addition to declaring methods as private, you can also:
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.