Analysis of Static classes and JSON in JavaScript

Source: Internet
Author: User

Here we will introduce the JavaScript static class and JSON. Many people doubt the relationship between JavaScript static and JSON. I believe you will have your own opinions after reading the full text.

Let's first look at a simple static class. There is no method for only public fields.

Var Person = {id: 1, name: "Jerry "};

I don't know whether you have found it is similar to the JSON format.

Var Person = {"id": 1, "name": "Jerry "};

We should have discovered the difference between a JSON object and a static class. The key name of a static class is a string with double quotation marks, while that of a static class is not a public field or method, however, there is no difference in usage. I don't understand why the JSON standard is like this, the following figure comes from the http://www.json.org/

I personally prefer the first format. Next I will introduce the static class as a verification tool class.
First, a regular enumeration is required.

 
 
  1. // Regular expression-it can be regarded as a static class or an enumeration class. 
  2. Var Regex = {
  3. Number:/^ \ d +/
  4. };

The following is the verification tool class. Of course it is not very powerful. I am just a small example here to illustrate the usage of JavaScript static classes.

 
 
  1. // Verification tool-static class 
  2. Var Validate = {
  3. // Icon-public field, I do not know how to implement private field 
  4. IconOk:"OK",
  5. IconError:"Error",
  6. // Static method-Enumeration 
  7. Match: function (regex, text ){
  8. // This keyword is required for internal use of fields. 
  9. If(Regex. test (text ))
  10. Document. getElementById ("Error"). InnerHTML =This. IconOk;
  11. Else 
  12. Document. getElementById ("Error"). InnerHTML =This. IconError;
  13. }
  14. };
  15.  
  16. "Text" Onblur = "Validate. match (Regex. number, this. value )" />
  17. "Error">
  1. Conversion from JavaBean to XML and JSON
  2. Detailed introduction to JSON-based advanced AJAX Development Technology
  3. Detailed description of the Java Bean application steps for JSP instances
  4. Call JavaBean in JSP
  5. JSP Servlet JavaBean built under jdk6.0 Tomcat6.0

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.