A simple example shows how to use JavaScript to create private variables and methods in a class.
Summary
Private variables are declared using the 'var' keyword inside the object, and they can only be accessed by private functions and privileged methods.
Private functions are declared in the object constructor (or through var functionName = function (){...} it can be called by privileged functions (including object constructor) and private functions.
The privileged method is declared through this. methodName = function () {...} and may be called by code outside the object. It can use the: this. privileged function () method to call a privileged function, and the Private function () method to call a private function.
Public attributes are defined by this. variableName and can be read and written outside the object. Private functions cannot be called.
Public methods are defined by ClassName. prototype. methodName = function () {...} and can be called from outside the object.
The prototype attribute is defined by ClassName. prototype. propertyName = someValue.
Static attributes are defined by ClassName. propertyName = someValue.
Note the following syntax: The var function name = function name () {...} has the special function and Private function features when called.
Example:
The Code is as follows: