SoFunction
Updated on 2025-04-06

javascript object-oriented programming talks about objects

Let’s first look at the JSON (javascript object notation) object. JSON is a data exchange format object commonly used in script operations. Compared with XML, JSON is a relatively lightweight format. In some intelligence IDEs, it can also conveniently operate members of JSON objects through points.

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

    var student = {
      Name: "Zhang San",
      Age: 20,
      Hobby: "read",
      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, which has name, age, hobbies, book collections, etc. When accessing this student object, students' information can be manipulated through the student variable.

    var stuInfo = "Name:" +  +
           ",age:" +  +
",Hobby:" +  +
           ", the book that owns:" +
                      [0].BookName + "、" +
           [1].BookName + "、" +
                      [2].BookName;
     alert(stuInfo);

This operation style is also very similar to C#. The above code statically constructs the student object, and the structure of the student object is determined. In other programming languages, the general object structure cannot be easily modified once it is determined, but the object structure in javascript can also be easily modified. Below is an Introduction method for the student object to introduce yourself.

     = function() {
      var stuInfo = "Name:" + this.Name +
             ",age:" + this.Age +
             ",Hobby:" + this.Hobby +
             ", the book that owns:" +
             this.Books[0].BookName + "、" +
             this.Books[1].BookName + "、" +
             this.Books[2].BookName;
      alert(stuInfo)
    };
    ();

The student object originally did not have an Introduction method. The first time the assignment will create a new member in the student object. If the assignment is made, the value assigned will be overwritten. Of course our value is a function. Members can also be added in an index-like manner.

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

Of course, the added members can also be deleted. After deletion, it becomes undefined, and it is not supported when accessing the member.

    delete ;
    ();
 

       

Javascript is a weak-type language. Sometimes even with the help of the IDE, it is not clear that the members of the currently operating object are currently operating. It may be necessary to query the properties of the current object. At this time, we can use a for loop to complete it.

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

       

When traversing the student object, it is to traverse the members of the student, and the key here corresponds to the attribute name of each member in the student object. student[key] is to access a member of student. If you want to call the student's Introduction method, you can also use the index method, student["Introduce"]().

The above briefly talks about JSON objects. In general, JSON is a very convenient way to package data. Other objects in javascript, whether they are browser objects, objects or arrays created by custom types, etc., also have similar operations methods for JSON objects. We can directly add members to window by indexing, and we can also add subscripts in string form to arrays to operate as Hashtable.

    window["Hi"] = function() {
      alert("helloworld!");
    };
    window["Hi"]();
 
    var array = [];
    array["one"] = "A";
    array["two"] = "B";
    array["three"] = "C";
    array["Four"] = "D";
    alert(array["one"] + array["two"] + array["three"] + array["Four"]);

When operating an array as a Hashtable, be careful not to add array elements to the array, but to add new attribute members to the array object. And if for(var key in array) loops through the array object, the key gets not the attribute name of the array object, but the index number of the array element.

Let’s talk about function next time.