How To Call Private?

In a class, define the method as private (e.g., `private foo() { … }`).

Call it from within the same class using `this.foo()` or `foo()` (depending on how you reference scope).

In TypeScript, call it only from inside the declaring class; calling it from outside will fail at compile time.

In JavaScript (ES private fields/methods), call it only within the declaring class; outside access is not allowed.

Use the class’s public method(s) to indirectly trigger the private method (e.g., a `public bar()` method that calls `this.foo()`).

If you need external access, expose a public wrapper method that calls the private method.

Suggested for You

Trending Today