Chapter 3 of msajax-js creation class (1)

Source: Internet
Author: User

1. Ajax type used

2. Implementation

3. Notes

4. Use Cases

We need to use Ajax js to write an object-oriented class. First, we must understand the type class in Ajax. when defining a class, we need to define the namespace of this class, when writing a JS script, it is often difficult or difficult to write a file similar to a namespace. When using a JS script, it is likely that multiple scripts will be referenced with duplicate names, as a result, the program cannot run due to name conflicts. msajax provides us with a convenient method for registering a namespace-type. registernamespace ().

Next, create a class and her constructor, define the class field, and pass the parameter to the class field.

Fww. Person = function (ID, name)
{
This. _ id = ID;
This. _ name = Name;
}

Step 3: Create a class attribute. Put the attribute in class. Prototype. Get indicates that the class is obtained, set indicates that the class is obtained, and the method is the same as the property but does not need get, Set

Fww. Person. Prototype =
{
Get_id: function ()
{
Return this. _ id;
},
Get_name: function ()
{
Return this. _ name;
},
Set_name: function (value)
{
If (value! = NULL)
{
This. _ name = value;
}
},
Tostring: function ()
{
Return string. Format ("Hello, I am {0}, my employee ID is: {1}", this. get_name (), this. get_id ());
}
}

Step 4 register the class to msajax

Fww. Person. registerclass ("fww. person ");

This object-oriented class is successfully created,

Note:

1. when defining attributes and methods, we recommend that you use get and set. In fact, this is not distinguished inside Js. You don't need get, it has nothing to do with set, mainly to make others or themselves better understand the meaning of this function. This is a standard.

2. when defining a class field, I do not have an image.. Net displays the defined class field outside the constructor, but uses this. filedname, which is feasible in this case because JS is a weak type language that allows this. This. _ id is equivalent to defining var ID in the class.

3. Do not forget to add () when calling class attributes! You may use it. net is used to person. get_name is called. It is wrong when JS is like this. I have already mentioned that in JS, all the real calls of the function method must be added regardless of the time attribute and method () -person. get_name.

4. When writing the Set Method of an attribute, you should check whether the value is null. Of course, this is just something I suggest you do not write it.

Complete use case:

Type. registernamespace ("fww ")

Fww. Person = function (ID, name)
{
This. _ id = ID;
This. _ name = Name;
}
Fww. Person. Prototype =
{
Get_id: function ()
{
Return this. _ id;
},
Get_name: function ()
{
Return this. _ name;
},
Set_name: function (value)
{
If (value! = NULL)
{
This. _ name = value;
}
},
Tostring: function ()
{
Return string. Format ("Hello, I am {0}, my employee ID is: {1}", this. get_name (), this. get_id ());
}
}

Fww. Person. registerclass ("fww. person ");

So much. In chapter 3 of msajax-js creation class (2), how do you write an inheritance class.

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.