Javascript object-oriented programming function is also class _ js object-oriented

Source: Internet
Author: User
Function is used to create functions or methods in javascript. To implement object-oriented programming, class is an indispensable role and the main character. However, javascript does not have the concept of a class. The so-called class is also simulated. The class members and Private Members are simulated through the function plus the closure (for details about the closure, refer to crossing the boundary: closure ). Here we will look at the "class" in javascript in a relatively solid way to avoid some blunt principles.

Since the function is used to simulate the class, write the code to create the class keyword or function. We create a coordinate class.

    function Point() {
      this.X = 0;
      this.Y = 0;
    };
 
    var zeroPoint = new Point();
Alert ("Current coordinate value (X :"+ ZeroPoint. X +", Y :"+ ZeroPoint. Y +")");

As we all know, the access of non-static class members needs to be completed through objects. Therefore, a new Point type object is created first, and then the X and Y coordinate values are accessed through this object. In terms of syntax, the creation process of javascript class objects is similar to that of C #, but the implementation mechanism is different. Here, a new Object is created, and this is pointed to this new Object during subsequent Point () function execution. In Point, this. X and this. Y are two public members of the Point class. Therefore, Point objects can directly access them.

When it comes to class members or object members, it is inevitable to mention the accessibility issue. In javascript classes, there are also public and private members, but the details are different. Javascript private members are also members that cannot be operated by objects outside the class. In fact, Private Members within the class cannot be accessed. In a class, only private members and Private Members can access each other. You can think that other Members have insufficient permissions to operate on these private things, but if you have the privilege, that's different. It's a private public image. Private member variables are the same as common variable declarations. Using the var keyword, private methods can use var declaration variables to receive method objects, and can also be constructed as common methods.

    function Lady() {
      var age = 30;
      VarName ="Cauliflower";
 
      var think = function() {
Alert ("Actually, this year"+ Age +"Years Old. ");
      };
      
      function fancy(){
Alert ("Fantasy is 20 years old. ");
      };
 
      this.Introduce = function() {
Alert ("My name is"+ Name +", 20 years old. ");
      };
    };
 
    var younglady = new Lady();
Alert (younglady. age );// Result undefined
Younglady. think ();// Not supported
Younglady. fancy ();// Not supported

The above is a Lady class. age, think, and fancy are all private members. The think and fancy methods can access the age and name, think, and fancy methods, and can also call each other. However, they are private, so the created youngLady object cannot call age, think, and fancy, and of course cannot call name. If private members can only call each other, the meaning of the existence of private members is actually lost. Javascript provides privileged members to build bridges between external and private members. Privileged members are public members, including common public members, privileged members, and object public members.

Privileged members are members created using this. XX in the class. They can be called through objects. They can also access private members and establish a channel for Private Members to be accessed.

    function Lady() {
      var age = 30;
      This. Name ="Cauliflower";
 
      var think = function() {
Alert ("Actually, this year"+ Age +"Years Old. ");
      };
 
      function fancy() {
Alert ("Fantasy is 20 years old. ");
      };
 
      this.Introduce = function() {
Alert ("My name is"+This. Name +", This year"+ Age +"Years Old. ");
      };
    };
 
    var younglady = new Lady();
    younglady.Introduce(); //Introduce

The creation of common public members is not encoded in the class, but created through the prototype of the class. Adding common public members is directly added to the prototype of the class, and prototype is a member set object like a JSON object. When we create an Object, we can think that the Members in the prototype class will be copied to the new Object.

    var younglady = new Lady();
    younglady.Introduce(); //Introduce
 
Lady. prototype. holobby ="Surfing the Internet";
    Lady.prototype.GetName = function() {
      return this.Name;
    };
    
    var lady2 = new Lady();
    alert(lady2.GetName());
    alert(lady2.Hobby);

The code above adds the common public member GetName method and holobby attribute for the Lady class through prototype. Because it is a public member, they can access each other with the privileged Member originally defined in the class. Because public members can access each other. Modify the preceding code. As follows.

    var younglady = new Lady();
 
Lady. prototype. holobby ="Surfing the Internet";
    Lady.prototype.GetName = function() {
      return this.Name;
    };
 
    alert(younglady.GetName());
    alert(younglady.Hobby);

Create a Lady object first, and then modify the class members. The previously created object also has a new member. This is the benefit of prototype as a kind of prototype. It is easy to understand here that prototype is a kind of Class Object template, and modifications to the template will affect all class objects.

When adding common members, you can add them in batches and assign them to prototype using a new JSON object. However, note that the original prototype is replaced. The created object references the old prototype object, therefore, the objects created before prototype replacement do not have holobby and GetName members.

    Lady.prototype = {
Holobby:"Surfing the Internet",
      GetName: function() {
        return this.Name;
      }
    };
    var younglady = new Lady();
    alert(younglady.GetName());
    alert(younglady.Hobby);

In addition to adding public members when building a class, you can also perform member operations on objects directly. This is described in the second article in this small series. Here we will add that the Members directly added to the object are also public members. These members can also be accessed with the first public members in the class.

    younglady.SetName = function(name) {
      this.Name = name;
    };
Younglady. SetName ("Cheiming");
    alert(younglady.GetName());

The above describes what class members are And next we will talk about what is related to class inheritance. (Correct your suggestion if any)

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.