Public, private, and privileged modes of JavaScript

Source: Internet
Author: User

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:
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> </title>
<Script type = "text/javascript">
Function Container (param ){
Function dec (){
If (secret> 0 ){
Secret-= 1;
SetSecret (7)
Alert (secret );
Return true;
}
Else {
// Alert ("over" + this. member );
Return false;
}
}
// This. dec = dec;
// This. dec = function dec () {...} different from above code.
Function setSecret (num ){
Secret = num;
}
This. member = param;
Var secret = 3;
Var self = this;
This. service = function (){
If (dec ()){
Alert ();
}
Else {
Return null;
}
}
}
//---------------------------------------
Function start (){
Alert ("Start ")
Var test = new Container ('liuqi ');
// Test. setSecret (2 );
Test. service ();
Test. service ();
Test. service ();
Test. service ();
Var test2 = new Container ('liuqi ');
// Test2.service ();
// Container. dec ();
}
</Script>
</Head>
<Body>
<Div onclick = "start ()" style = "color: blue"> click me </div>
</Body>
</Html>

JavaScript is the most misunderstood programming language in the world. Some people think that it lacks the Information Hiding feature because JavaScript objects cannot have private variables.
However, this is a misunderstanding. JavaScript objects can have private members.
Object
JavaScript is basically about objects. Arrays are objects, methods are objects, and objects are objects. What is an object? An object is a set of key-value pairs. The key is a string,
The value can be a string, number, Boolean, and object (including arrays and methods ). Objects are usually implemented as Hashtable, so that values can be quickly obtained.
If the value is a function, I can call it a method. When an object's method is called, the "this" variable is assigned to the object. You can use the "this" variable to access the instance.
Variable.
The object can be generated by the constructor, the method used to initialize the object. Constructors provide features provided by classes in other programming languages, including static variables and methods.
Public
All objects are public members. Any object can access, modify, delete, or add new members. There are two main ways to place members in a new object:
In the constructor
This technique is usually used to initialize public instance variables. The "this" variable of the constructor is used to add members to an object.
Java code
Copy codeThe Code is as follows:
Functin Container (param ){
This. member = param;
}
Functin Container (param ){
This. member = param;
}

In this way, if we construct a new object var myContainer = new Container ('abc'), myContainer. member is 'abc '.
In prototype
This technique is usually used to add public methods. If you are looking for a member and it is not in the object itself, you can find it from the prototype member of the object's constructor.
The prototype mechanism is used for inheritance. To add a method to all objects created by the constructor, you only need to add it to the prototype of the constructor:
Java code
Copy codeThe Code is as follows:
Container. prototype. stamp = function (string ){
Return this. member + string;
}
Container. prototype. stamp = function (string ){
Return this. member + string;
}

In this way, we can call this method myContainer. stamp ('def ') and the result is 'abcdef '.
Private
Private Members are generated by constructors. Common var variables and constructor parameters are called private Members.
Java code
Copy codeThe Code is as follows:
Function Container (param ){
This. member = param;
Var secret = 3;
Var that = this;
}
Function Container (param ){
This. member = param;
Var secret = 3;
Var that = this;
}

This constructor creates three private instance variables: param, secret, and that. They are added to an object, but cannot be accessed externally or by the object's own
Public method access. They can only be accessed by private methods. The private method is the internal method of the constructor.
Java code
Copy codeThe Code is as follows:
Function Container (param ){
Function dec (){
If (secret> 0 ){
Secret-= 1;
Return true;
} Else {
Return false;
}
}
This. member = param;
Var secret = 3;
Var that = this;
}
Function Container (param ){
Function dec (){
If (secret> 0 ){
Secret-= 1;
Return true;
} Else {
Return false;
}
}
This. member = param;
Var secret = 3;
Var that = this;
}

The private method dec checks the secret instance variables. If it is greater than 0, secret is reduced and true is returned. Otherwise, false is returned. It can be used to limit this object to three times.
By convention, we define a private that variable. This is used to allow the private method to use this object. This is because the ECMAScript language specification is incorrect,
This error causes you to incorrectly set this to the internal method.
Private methods cannot be called by public methods. To make the private method useful, we need to introduce the privileged method.
Privileged
The privileged method can access private variables and methods, and it can be accessed by the public method and the outside world. You can delete or replace the privileged method, but not
Change it or force it to disclose its own secrets.
The privileged method uses this allocation in the constructor.
Java code
Copy codeThe Code is as follows:
Function Container (param ){
Function dec (){
If (secret> 0 ){
Secret-= 1;
Return true;
} Else {
Return false;
}
}
This. member = param;
Var secret = 3;
Var that = this;
This. service = function (){
If (dec ()){
Return that. member;
} Else {
Return null;
}
};
}
Function Container (param ){
Function dec (){
If (secret> 0 ){
Secret-= 1;
Return true;
} Else {
Return false;
}
}
This. member = param;
Var secret = 3;
Var that = this;
This. service = function (){
If (dec ()){
Return that. member;
} Else {
Return null;
}
};
}

Service is the privileged method. If myContainer. service () is called three times before, 'abc' is returned '. Then, it returns null. Service calls the private dec method,
The dec method accesses the private secret variable. The service is visible to other objects and methods, but it does not allow direct access to private variables.
Closure
Because JavaScript has closures, public, private, and privileged Member modes, it is feasible. This means that an internal method can always access its external Method
Var variables and parameters, even after the external method returns. This is a very powerful feature of the JavaScript language. No JavaScript code showing how to explore this feature
Programming books are not even mentioned.
Private and privileged members can only be generated when the object is constructed. Public members can be added at any time.
Mode
Public
Java code
Copy codeThe Code is as follows:
Function Constructor (...){
This. membername = value;
}
Constructor. prototype. membername = value;
Function Constructor (...){
This. membername = value;
}
Constructor. prototype. membername = value;

Private
Java code
Copy codeThe Code is as follows:
Function Constructor (...){
Var that = this;
Var membername = value;
Function membername (...){...}
}
// Note: function statement
// Function membername (...){...}
// Is short for the following code
// Var membername = function membername (...){...};
Function Constructor (...){
Var that = this;
Var membername = value;
Function membername (...){...}
}
// Note: function statement
// Function membername (...){...}
// Is short for the following code
// Var membername = function membername (...){...};

Privileged
Java code
Copy codeThe Code is as follows:
Function Constructor (...){
This. membername = function (...){...};
}
Function Constructor (...){
This. membername = function (...){...};
}

Note: I think the privileged method can be simply regarded as the public method in the constructor, because the privileged method can be accessed by external and public methods,
And it can access private variables.

Related Article

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.