Javascript Object-Oriented Programming

Source: Internet
Author: User

Let's take a look at the JSON (javascript object notation) object. JSON is a type of data exchange format object commonly used in script operations. JSON is a lightweight format compared to XML, in some intelligence ides, you can also easily operate on members in JSON objects by clicking.

JSON is a key/value pair to describe the format of internal members. Its internal members can be almost any type of objects, of course, methods, classes, and arrays, it can also be another JSON object.

    var student = {
Name:"James",
      Age: 20,
Holobby:"Reading",
      Books: [
        {
          BookName : "C#" ,
          Price : 70
        },
        {
          BookName : "Java" ,
          Price : 70
        },
        {
          BookName : "Javascript" ,
          Price : 80
        }
      ]
    };

The above Code uses a JSON object to describe a student's information, including name, age, hobby, and book set. When accessing this student object, you can use the student variable to manipulate student information.

    VarStuInfo ="Name :"+ Student. Name +
           ", Age :"+ Student. Age +
", Hobbies :"+ Student. holobby +
           ", Book :"+
                      student.Books[0].BookName + "、" +
           student.Books[1].BookName + "、" +
                      student.Books[2].BookName;
     alert(stuInfo);

The style of this operation is very similar to that of C. The above code is a static structure of the student object, and the structure of the student object is determined. In other programming languages, the object structure cannot be easily modified once determined, but the object structure in javascript can also be easily modified. The following describes how to add an Introduce Method to the student object.

    student.Introduce = function() {
      VarStuInfo ="Name :"+This. Name +
             ", Age :"+This. Age +
             ", Hobbies :"+This. Holobby +
             ", Book :"+
             this.Books[0].BookName + "、" +
             this.Books[1].BookName + "、" +
             this.Books[2].BookName;
      alert(stuInfo)
    };
    student.Introduce();

The student object does not have the Introduce method. It is student for the first time. the Introduce assignment creates a new member in the student object. the value of Introduce overwrites the value of the previous assignment. Of course, our value is a function. You can also use an index-like method to add members.

    student["Introduce"] = function() {
     ……
    };
 
    student.Introduce();

Of course, the added members can also be deleted. It becomes undefined after deletion, and cannot be accessed again.

    delete student.Introduce;
    student.Introduce();
 

Javascript is a weak type language. Sometimes, even with IDE assistance, you cannot clearly know the members of the currently operated object. You may need to query the attributes of the current object, in this case, we can use the for loop.

    for (var key in student) {
      document.write(key + " : " + student[key] + "<br />");
    };

The student object is traversed by the student member. The key here corresponds to the attribute name of each member in the student object. Student [key] is used to access a member of student. If you want to call student's Introduce method, you can also use the index method, student ["Introduce"] ().

In general, JSON is a convenient data packaging method. Other Objects in javascript, whether browser objects, objects created by custom types, or arrays, all have similar operations for JSON objects. We can directly add members to the window by indexing, or we can add a string subscript to the array to treat it as Hashtable.

    window["Hi"] = function() {
      alert("helloworld!");
    };
    window["Hi"]();
 
    var array = [];
Array ["1"] ="";
Array ["2"] ="B";
Array ["3"] ="C";
Array ["4"] ="D";
Alert (array ["1"] + Array ["2"] + Array ["3"] + Array ["4"]);

When using arrays as Hashtable, you should note that you do not add array elements to arrays, but add new attribute members to array objects. If for (var key in array) cyclically traverses the array object, the key does not obtain the attribute name of the array object, but the index number of the array element.

Next time you talk about function.

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.