Private Methods

Private methods improve component security and encapsulation by restricting access to internal logic. Private methods aren't part of the inheritance chain and are inaccessible to subclasses or external callers. Attempting to access a private method outside its class context results in a compile-time SyntaxError.

To use private methods, opt in to the Dev channel in Salesforce Release Manager

The Dev Channel provides access to features that are not generally available and have been designated as pilot, beta, limited release, or developer preview. Their use is at the Customer’s sole discretion and is subject to the Beta Services Terms at Agreements - Salesforce.com.

To make a method private, prefix the method with a # hash. Private methods are useful for internal logic that you don't want to expose as part of the component's public API.

Private methods can help to prevent parent components from reaching into the child component's internals, making your code more maintainable and less prone to breaking changes.

Having a public method and a private method with the same name is supported, for example, myMethod() and #myMethod(). For readability, it's better to use unique, descriptive names for your private helpers.

Consider these guidelines when working with private methods.

  • Call a private method within the same class only.
  • Accessing the private method from outside the class context isn't allowed.
  • Referencing the private method from the template isn't allowed.

Additionally, consider these restrictions.

  • Decorators such as @api and @track can't be applied to private methods.
  • Private methods can't start with reserved prefix __lwc_.
  • Private fields are currently not supported.
  • Private getters and setters are currently not supported.

See Also