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();