JavaScript Object-oriented programming second, talk about objects.

Source: Internet
Author: User
Tags json

JavaScript is an object-based programming language. From window to document, from method to class, object to array are objects.

Looking at the JSON (JavaScript Object notation) object, JSON is a data Interchange Format object commonly used in scripting operations, and JSON is a lightweight format relative to XML, in some The intelligence IDE also makes it easy to manipulate the members of the JSON object by point.

JSON is a key/value pair that describes the format of an inner member that can be an object of almost any type, or a method, class, array, or another JSON object.

var student = {
       Name: "张三",
       Age: 20,
       Hobby: "读书",
       Books: [
         {
           BookName : "C#" ,
           Price : 70
         },
         {
           BookName : "Java" ,
           Price : 70
         },
         {
           BookName : "Javascript" ,
           Price : 80
         }
       ]
};

The above code describes a student's information with a JSON object, with a name, age, Hobby, book set, and so on. When accessing the Student object, you can manipulate the student's information by student variables.

var stuInfo = "姓名:" + student.Name +
            ",年龄:" + student.Age +
            ",爱好:" + student.Hobby +
            ",拥有的书:" +
                       student.Books[0].BookName + "、" +
            student.Books[1].BookName + "、" +
                       student.Books[2].BookName;
alert(stuInfo);

This style of operation is very similar to C #. The above code is static constructs the student object, the student object's structure is determined. In other programming languages the general object structure is not easily modified once it is determined, but the object structure in JavaScript can be easily changed. Next, add a introduce method to the student object to introduce yourself.

student.Introduce = function() {
       var stuInfo = "姓名:" + this.Name +
              ",年龄:" + this.Age +
              ",爱好:" + this.Hobby +
              ",拥有的书:" +
              this.Books[0].BookName + "、" +
              this.Books[1].BookName + "、" +
              this.Books[2].BookName;
       alert(stuInfo)
};
student.Introduce();

Student object originally did not introduce method, the first time for student. The introduce assignment creates a new member in the student object, followed by student. Introduce assignment overrides the value assigned the previous time. Of course, our value here is a function. You can also add members in an index-like manner.

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

student.Introduce();

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

delete student.Introduce;
student.Introduce();
 

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.