Private Method: A private method can be set to all the attributes inside the category, that is, private and public attributes. However, private methods cannot be called outside the class.
Private method Syntax:
Copy codeThe Code is as follows:
Function myClass (){
Var private_attribute = initial_value;
Function private_method (){}
Var private_method2 = function (){}
}
Instance showpet () is a private Method
Copy codeThe Code is as follows:
Var pet = function () {var temp = "" // Private variables can only be accessed within the scope of the function or object
Function showpet (){
Alert ("123 ")
}
Showpet (); // Private methods can be used within the scope of the function.
}
Showpet (); // Error
Pet. showpet () // still cannot be called like this
Var Penguin = new pet () // instantiate a pet object
Penguin. showpet () // sorry, this still cannot be called.
Public method:
1. Public methods can be called outside the class,
2. However, it cannot be a private attribute of the category class.
3. Public methods must be added either inside or outside the class through the prototype attribute of the class.
Public method Syntax:
Copy codeThe Code is as follows:
Function myClass (){
This. public_attribute = initial_value;
This. prototype. public_method = function (){}
}
MyClass. prototype. public_attribute2 = initial_value;
MyClass. prototype. public_method2 = function (){}
Instance:
Copy codeThe Code is as follows:
Var pet = function (){
Function showname () {// Private Method
Alert (this. name)
}
This. show = function () {// if you do not understand it here, please note that this method will be introduced below.
Showname ();
}
}
Pet. prototype. setname = function (str ){
Name = str;
}
Var Penguin = new pet ()
Penguin. setname ("Penguin"); // Add the Instance name to Penguin
Penguin. show (); // pop up Penguin
Penguin. setname ("wind"); // The name Of The added instance is wind
Penguin. show (); // The wind is displayed.
Privileged method:
1. Privileged methods can be called outside the class,
2. But it can be a private property of the category and a public property of the category, and it can be barely considered a special public method.
3. But it is different from the Declaration and definition of the public method above. Privileged methods must be declared and defined within the class.
Privileged method Syntax:
Copy codeThe Code is as follows:
Function myClass (){
This. privileged_method = function (){}
}
Instance
Copy codeThe Code is as follows:
Var pet = function (){
Function showname () {// Private Method
Alert (this. name)
}
This. show = function () {// defines a privileged method by using the this keyword.
Showname (); // access the private method in the privileged method;
}
}
Pet. prototype. setname = function (str ){
Name = str;
}
Var Penguin = new pet (); // instantiate a pet object
Penguin. setname ("Penguin"); // call the public method to modify
Penguin. show (); // call the privileged method to access the private method. The name is displayed.
Below are some of my own understandings: Through the above learning, combined with my own books. The public, private, and privileged permissions are understood as follows:
Public method: all objects instantiated through this class have or can be used together. The common methods are generally put in the "prototype object". If they are put in the constructor, the common methods are created again.
Private method: it cannot be called externally.
Privileged method: the closure principle is used to allow internal functions to access variable objects (private variables and private methods) of external functions through the scope chain ). (Scope chain, closure, variable object; these three are explained in Javascript advanced programs)