JavaScript Object-oriented programming talk about objects _js object-oriented

Source: Internet
Author: User
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 intelligence Ides, you can also easily manipulate the members of a JSON object by using the dot.

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:  "John",  
 Age:20, 
 Hobby:  "reading",  
 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 =  "name:" + student. Name +   
  
 
  
                       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 =  last Name Name: "+ 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[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 ();
 
 

JavaScript is a weakly typed language, and sometimes even with the help of the IDE it is not clear that the members of the object currently being manipulated may need to query the properties of the current object, which we can do with the For loop.

    For (instudent) {
      "<br/>");
    };

When the student object is traversed, the member of the student is traversed, and the key here corresponds to each member property name in the student object. Student[key] is accessed by a member of the student. If you want to invoke the student introduce method, you can also student["introduce" () by indexing.

      A simple chat about JSON objects, in general JSON is a convenient way to package data. Other objects in JavaScript, whether they are browser objects, objects created by custom types, or arrays, and so on, have a similar way of working with JSON objects. We can add a member to the window directly by index, and we can also add the subscript as a string in the array to manipulate it as a hashtable.

 Window[ "Hi"] = function () {
 /span> 
 alert ( "helloworld!");  
 
 window[ "Hi"] ();  
&NBSP; 
 var array = [];  
 Array[ "one"] =  "a";   
 array[ "two"] =  "B";   
 Array[ "three"] =  "C";   
 array[ "four"] =  "D";   
 alert (array[ "one"] + array[ "two"] + array[ "three"] + Array[ "four"]);     

When you manipulate an array as a Hashtable, be aware that instead of adding an array element to an array, you add a new property member to a set of objects. And if the for (var key in array) loops through the array object, the key gets not the array object's property name, but the index number of the array element.

Next time, talk about function.

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.