JavaScript public, private, and privileged pattern _ javascript skills

Source: Internet
Author: User
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:





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.