In JS Code Write function FN () {} Or var fn = function () {} and so on, you can understand it as an object, of course, there are arrays and so on.
Before understanding object-oriented, let's take a look at the following.
1. Call of object Methods
The function written at the outermost layer of JS can also be understood as a method of window objects. The Defined variables can also be called an attribute of the window object. For example:
[JavaScript] View plaincopy
-
- VaRTest =Function(){
-
- Alert ("123")
- }
-
- Of course, you can also define
-
- FunctionTest (){
-
- Alert ("123")
-
- }
-
- We can call this method as a window object.
-
- Test ()// Pop-up warning box 123 (because the window object is a top-level object, we can omit it)
-
- Window. Test ()// Pop-up warning box 123
-
- Window ['Test'] ()// Pop-up warning box 123, which can be understood as a method value under the window array object.
The above example shows how to use and call object methods.
2. Private Method
Private methods can only be used within the scope of an object. This can be understood in the form of variable scope.
The functions in the above example can be considered as the private method of the window object. Continue with the example below.
[JavaScript] View plaincopy
- VaRPET =Function(){
-
- FunctionShowpet (){
-
- Alert ("123")
-
- }
-
- Showpet ();// Private methods can be used within the function scope.
-
- VaRTemp ="Private variables can only be accessed within the scope of a function or object"
-
- }
- Showpet ();// Error
-
- Pet. showpet ()// Still cannot be called like this
-
-
- VaRPenguin =NewPET ()// Instantiate a pet object
-
- Penguin. showpet ()// Sorry, this still cannot be called.
-
- VaRPET =Function(){
-
- FunctionShowpet (){
- Alert ("123")
-
- }
-
- Showpet ();// Private methods can be used within the function scope.
-
- VaRTemp ="Private variables can only be accessed within the scope of a function or object"
-
- }
-
- Showpet ();// Error
-
- Pet. showpet ()// Still cannot be called like this
-
- VaRPenguin =NewPET ()// Instantiate a pet object
-
- Penguin. showpet ()// Sorry, this still cannot be called.
What if I want to define a method that can be called outside the scope of the object? How can I use a private method? Let's take a look at the next point.
3. Static Method
Let's continue with the above example with the above question.
[JavaScript] View plaincopy
-
- VaRPET =Function(){
- FunctionShowpet (){// Private Method
-
- Alert ("123")
-
- }
-
- Showpet ();// Private methods can be used within the function scope.
-
- }
-
- Pet. Show =Function(){// Add a static method to the pet object.
-
- Alert ("456")
-
- }
- Pet. Name ="Penguin"// Add a static property to the pet object.
-
-
- Pet. Show ()// Pop-up warning box 456
-
- Alert (pet. Name)// The prompt box penguin is displayed.
-
-
- // Continue thinking collision
-
- ==================================
-
- VaRPenguin =NewPET ()// Instantiate a pet object
- Penguin. Show ()// Sorry, this static method does not seem to be inherited by the instance. It is commendable to have such ideas. Come on!
The above example shows you what static method is. Of course, you may not understand it. In fact, I don't understand it either, because I am also a cainiao, however, if you know how to write a static method for an object, you can call the static method. Maybe one day, you will come back to teach me. Let's take a look at the methods that can be called by instantiated objects with the above questions.
4. Public Methods
The public method is usually implemented by modifying the prototype of the constructor. After an object prototype is modified, all the object instances inherit the modifications of the prototype, if you feel fuzzy, ignore it ).
Modify the object prototype. Continue with the above example.
Pet. Prototype. setname = Function (STR ){ // Add a public method by modifying the prototype to add and modify the name of the Instance Object
Name = STR;
}
Example:
[JavaScript] View plaincopy
-
- VaRPET =Function(){
- FunctionShowname (){// 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;
-
- }
-
- VaRPenguin =NewPET ()
-
- Penguin. setname ("Penguin");// The Name Of The added instance is penguin.
-
- Penguin. Show ();// Pop up Penguin
-
- Penguin. setname ("Wind");// The Name Of The added instance is wind.
- Penguin. Show ();// Wind
-
-
Run the code to play.
[JavaScript] View plaincopy
-
- <MCE: Script Type ="Text/JavaScript"> <! --
- VaRPET =Function(){
-
- Name:"123",
-
- FunctionShowname (){// Private Method
-
- Alert (This. Name)
-
- }
-
- This. Show =Function(){
-
- Showname ();
- }
-
- }
-
- Pet. Prototype. setname =Function(STR ){
-
- Name = STR;
-
- }
-
- VaRPenguin =NewPET ()
-
- Penguin. setname ("Penguin");
-
- Penguin. Show ();
-
- Penguin. setname ("Wind");
-
- Penguin. Show ();
- // --> </MCE: SCRIPT>
5. Privileged Methods(External interfaces of objects or functions)
In fact, we have used this method in the above example. This method can be called by instantiating Object Inheritance. Methods defined by using the thsi keyword within the constructor. Privileged methods can be publicly accessed outside the constructor (only for instantiated objects), and private members and methods can be accessed. Therefore, the interface used as an object or constructor is the most suitable, through privileged functions, we can control the access of public methods to private methods, which has many applications in JS framework extension.
You can refer to the following section as P. Let's take a look at how to define a privileged method, reference a privileged method, and continue to call the above example.
[C-sharp] View plaincopy
-
- VaR PET = function (){
-
- Function showname (){// Private Method
- Alert (This. Name)
-
- }
-
- This. Show = function (){// Use the this keyword to define a privileged method.
-
- Showname ();// Access the private method in the privileged method;
-
- }
-
- }
-
- Pet. Prototype. setname = function (STR ){
-
- Name = STR;
-
- }
- VaR penguin =NewPET ();// Instantiate a pet object
-
- Penguin. setname ("Penguin");// Call the public method for Modification
-
- Penguin. Show ();// Call the privileged method to access the private method. The name is displayed.
First, you can use this. fn = function () {} in the constructor to create a privileged method. Access private methods in privileged functions;
The instantiated object can use some private methods by accessing the privileged functions. The methods for accessing the privileged functions are the same as those for accessing the public functions.