Javascript object-oriented-static method-Private method-public method-privileged method

Source: Internet
Author: User
Tags what static method
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

  1. VaRTest =Function(){
  2. Alert ("123")
  3. }
  4. Of course, you can also define
  5. FunctionTest (){
  6. Alert ("123")
  7. }
  8. We can call this method as a window object.
  9. Test ()// Pop-up warning box 123 (because the window object is a top-level object, we can omit it)
  10. Window. Test ()// Pop-up warning box 123
  11. 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

  1. VaRPET =Function(){
  2. FunctionShowpet (){
  3. Alert ("123")
  4. }
  5. Showpet ();// Private methods can be used within the function scope.
  6. VaRTemp ="Private variables can only be accessed within the scope of a function or object"
  7. }
  8. Showpet ();// Error
  9. Pet. showpet ()// Still cannot be called like this
  10. VaRPenguin =NewPET ()// Instantiate a pet object
  11. Penguin. showpet ()// Sorry, this still cannot be called.
  12. VaRPET =Function(){
  13. FunctionShowpet (){
  14. Alert ("123")
  15. }
  16. Showpet ();// Private methods can be used within the function scope.
  17. VaRTemp ="Private variables can only be accessed within the scope of a function or object"
  18. }
  19. Showpet ();// Error
  20. Pet. showpet ()// Still cannot be called like this
  21. VaRPenguin =NewPET ()// Instantiate a pet object
  22. 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

  1. VaRPET =Function(){
  2. FunctionShowpet (){// Private Method
  3. Alert ("123")
  4. }
  5. Showpet ();// Private methods can be used within the function scope.
  6. }
  7. Pet. Show =Function(){// Add a static method to the pet object.
  8. Alert ("456")
  9. }
  10. Pet. Name ="Penguin"// Add a static property to the pet object.
  11. Pet. Show ()// Pop-up warning box 456
  12. Alert (pet. Name)// The prompt box penguin is displayed.
  13. // Continue thinking collision
  14. ==================================
  15. VaRPenguin =NewPET ()// Instantiate a pet object
  16. 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

  1. VaRPET =Function(){
  2. FunctionShowname (){// Private Method
  3. Alert (This. Name)
  4. }
  5. This. Show =Function(){// If you do not understand it here, please note that this method will be introduced below.
  6. Showname ();
  7. }
  8. }
  9. Pet. Prototype. setname =Function(STR ){
  10. Name = STR;
  11. }
  12. VaRPenguin =NewPET ()
  13. Penguin. setname ("Penguin");// The Name Of The added instance is penguin.
  14. Penguin. Show ();// Pop up Penguin
  15. Penguin. setname ("Wind");// The Name Of The added instance is wind.
  16. Penguin. Show ();// Wind

Run the code to play.

[JavaScript] View plaincopy

  1. <MCE: Script Type ="Text/JavaScript"> <! --
  2. VaRPET =Function(){
  3. Name:"123",
  4. FunctionShowname (){// Private Method
  5. Alert (This. Name)
  6. }
  7. This. Show =Function(){
  8. Showname ();
  9. }
  10. }
  11. Pet. Prototype. setname =Function(STR ){
  12. Name = STR;
  13. }
  14.  VaRPenguin =NewPET ()
  15. Penguin. setname ("Penguin");
  16. Penguin. Show ();
  17. Penguin. setname ("Wind");
  18. Penguin. Show ();
  19. // --> </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

  1. VaR PET = function (){
  2. Function showname (){// Private Method
  3. Alert (This. Name)
  4. }
  5. This. Show = function (){// Use the this keyword to define a privileged method.
  6. Showname ();// Access the private method in the privileged method;
  7. }
  8. }
  9. Pet. Prototype. setname = function (STR ){
  10. Name = STR;
  11. }
  12. VaR penguin =NewPET ();// Instantiate a pet object
  13. Penguin. setname ("Penguin");// Call the public method for Modification
  14. 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.

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.