How JavaScript declares objects

Source: Internet
Author: User

(1): construct a new object through the object scope of the new operator; then dynamically add attributes to build an object from scratch'

(2): define the "class" prototype of the object, and then use the new operator to create new objects in batches.

      
 // Define a Class address
   Function
Address (street, xno ){
    This. Street = Street
| 'A Road ';
    This. xno = xno
| 35;
    This. tostring = function (){

    

     Return
"Street ----" + this. Street + ", xno ---" + this. xno;
    }

   }
   // Define a class person

   Function
Person (name, age, ADDR ){
    This. Name = Name
| "Unknow ";
    This. Age = age;

    This. ADDR = ADDR
| New address (null, null );
    

    This. getname = function (){

     Return
This. Name;
    }

    This. getage = function (){

     Return
This. Age;
    }

    This. getaddr = function (){

     Return
This. ADDR. tostring ();
    }

   }
   // Create an object using the new operator. Note that these two objects are relatively independent entities.

   VaR Jack = new
Person ('jack', 26, new address ('qinghairoad', 123 ));
   VaR AB = new
Person ('AB', 26 );
   Alert (Jack. getname ());

   Alert (Jack. getage ());

   Alert (Jack. getaddr ());

   Alert (AB. getname ());

   Alert (AB. getage ());

   Alert (AB. getaddr ());

  
You can use breakpoint debugging to check out... for "|", I don't really understand it. That's the default value. Please explain it...

(3): Use JSON to build

  JSON (JavaScript Object
Notation), that is, to represent an object literally, from simple to complex use this method.

   VaR
OBJ = {
    Name: "AB ",

    Age: 26,

    Birthday: New
Date (1984,12, 12 ),
    ADDR :{

     Street: "Qinghai
Road ",
     Xno: 135"

    }

   }

  
In this way, it is much simpler to add objects in the class than the second declaration class, which clearly expresses the structure of an object such as obj. In fact, most experienced JavaScript programmers prefer to use this representation method, including many JavaScript toolkit, such as jquery, and extjs, which use JSON in large quantities. JSON is actually a data exchange format between the front-end and the server. the front-end program sends a JSON object to the backend through Ajax. The server script parses the JSON object and restores it to the server object. Then, it performs some processing and reports the JSON object to the front-end, using the same data format can reduce the chance of errors.

 
JSON format data itself can also be recursive and can express complicated data formats at will. JSON is easy to write, that is, key-value pairs enclosed in curly brackets. Key-value pairs are separated by colons, and values can be arbitrary Javascript objects, such as simple objects such as string, Boolean, number, null, or complex objects such as date, object, and other custom objects .....

  
Another Application Scenario of JSON is: when a function has multiple return values, in the traditional object-oriented language, we need to organize an object and then return, javaScript does not need to be so troublesome.

 Function Point (left, top ){
   
 This. Left = left;
    
This. Top = top;
    
Return {X: This. Left, Y: This. Top };
  }

 Directly and dynamically construct a new anonymous object and Return OK. var
Pos = point (2, 3 );

Return objects in JSON format. This object can have any complicated structure and can contain function objects. In actual programming, we usually need to traverse a JavaScript Object,
We don't know anything about objects beforehand. We can use the syntax sugar in the form of for... in ....

   For (VAR
ItemIn JSON ){

  // Item is the key

  // JSON [item] is the value

}

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.