Object-oriented in Microsoft Ajax Library

Source: Internet
Author: User

Oo
· Improves reusability and maintainability
· Various mature design modes can be used

Namespace
· In addition to avoiding naming conflicts, it is also important to enableProgramYou can easily find the types and classes they need,
During development, I found that many programmers do not know what the namespaces start with, and then the classes in a namespace
The two service-related classes are placed in a namespace.CodeLevel
Chaotic, not to be maintained in the future.
Register namespace type. registernamespace ("namespace ");

Class
· Define Constructors
Class constructor is defined as Function
Function myfunction (){}
VaR myfun = new myfunction ();

· define a member
Microsoft Ajax library object-oriented system recognizes Methods Starting with get _ and SET _ as attributes
[Code demo ]:
mynamespace. myclass = function (param1 ,...) // define the constructor
{< br> This. _ myproperty = NULL;
}< br> // defines attributes and method members. Note that each member must be separated by commas (,).
mynamespace. myclass. prototype =
{< br> get_myproperty: function ()
{return this. _ myproperty ;},

Set_myproperty: function (value)
{This. _ myproperty = value ;},

Methodname: function (){}
}

· Registration

[Code demo ]:
Mynamespace. myclass = function (param1 ,...)
{
// Define the constructor
}
Mynamespace. myclass. Prototype =
{
// Define attributes and method members
}
Mynamespace. myclass. registerclass ('mynamespace. myclass'); // registration class

Use demo
[Code demo ]:
<Asp: scriptmanager id = "scriptmanager1" runat = "server">
</ASP: scriptmanager>
<Script language = "JavaScript" type = "text/JavaScript">

Type. registernamespace ("dongdenamespace ");

Dongdenamespace. worker = function (name)
{
This. _ name = Name? Name: "(unknown )";
This. _ bornyear = 0;
}

Dongdenamespace. Worker. Prototype =
{
Get_name: function ()
{
Return this. _ name;
},


Get_bornyear: function ()
{

Return this. _ bornyear;
},
Set_bornyear: function (value)
{
This. _ bornyear = value;

},


Getage: function ()
{
Return 2008-This. _ bornyear;

},

Getworkerdescript: function ()
{

Return string. format ("{0} age: {1}, born at: {2}", this. _ name, this. getage (), this. get_bornyear (); // This cannot be removed
}

}

Dongdenamespace. Worker. registerclass ("dongdenamespace. Worker ");


VaR worker = new dongdenamespace. Worker ("dongde ");
Worker. set_bornyear (1981); // pay attention to the attribute Assignment Method.
Alert (worker. getworkerdescript ());



</SCRIPT>

Abstract class
· Classes containing abstract methods are abstract classes.
· The Microsoft Ajax library object-oriented system calls an abstract class that contains methods that directly throw exceptions after running the program,
The method for directly throwing an exception is called the abstract method.

· Inheritance: calls the constructor of the parent class. classes with the parent class must call the constructor of the parent class (if not, the inheritance effect will be lost)
Call the constructor syntax of the parent class mynamespace. myclass. initializebase (this, [param1,...]);
[Param1,...] indicates the parameter name of the constructor of the parent class. Note that the parameter name is not a parameter value.

· The parent class must be provided during registration
Mynamespace. myclass. registerclass ("mynamespace. myclass", mynamespace. parentclass );

· Defining methods with the same name can overwrite the abstract methods of the parent class

· The tostring method cannot be inherited. The tostring method is a method that comes with the Javascript system. Both the parent class and subclass as objects have the tostring method,
But not inherited

· Call the method of the parent class mynamespace. myclass. callbasemethod (this, "methodname", [param1,...]); // methodname is the method name of the parent class
This. methodname (); // indicates that the methodname method of the subclass is called.

[Code demo ]:

<Asp: scriptmanager id = "scriptmanager1" runat = "server">
</ASP: scriptmanager>
<Script language = "JavaScript" type = "text/JavaScript">

Type. registernamespace ("dongdenamespace ");

Dongdenamespace. worker = function (name)
{
This. _ name = Name? Name: "(unknown )";
This. _ bornyear = 0;
}

Dongdenamespace. Worker. Prototype =
{
Get_name: function ()
{
Return this. _ name;
},


Get_bornyear: function ()
{

Return this. _ bornyear;
},
Set_bornyear: function (value)
{
This. _ bornyear = value;

},


Getage: function ()
{
Return 2008-This. _ bornyear;

},


Getworkerdescript: function ()
{

Return string. Format ("{0} age: {1}, born at: {2}", this. _ name, this. getage (), this. get_bornyear ());
},

// Abstract Method
Getsalary: function ()
{
Throw error. notimplemented ();

}
}




//////////////////////////////////////// /////////////////////////
Dongdenamespace. Manager = function (name)
{
Dongdenamespace. Manager. initializebase (this, [name]);

}

Dongdenamespace. Manager. Prototype =
{
Getsalary: function ()
{
Return 10000;

}

}

//////////////////////////////////////// //////////////////////////
Dongdenamespace. advancemanager = function (name)
{
Dongdenamespace. advancemanager. initializebase (this, [name]);

}

Dongdenamespace. advancemanager. Prototype =
{
Getworkerdescript: function ()
{

Return dongdenamespace. advancemanager. callbasemethod (this, "getworkerdescript") + "My salary is high ";
},

Getsalary: function ()
{
Return 18000;

}


}




Dongdenamespace. Manager. registerclass ("dongdenamespace. Manager", dongdenamespace. Worker );

VaR worker = new dongdenamespace. Manager ("dongde ");
Worker. set_bornyear (1981 );
Alert (worker. getworkerdescript () + "Salary:" + worker. getsalary ());



Dongdenamespace. advancemanager. registerclass ("dongdenamespace. advancemanager", dongdenamespace. Worker );
VaR worker2 = new dongdenamespace. advancemanager ("dongde2 ");
Worker2.set _ bornyear (1961 );
Alert (worker2.getworkerdescript () + "Salary:" + worker2.getsalary ());

</SCRIPT>

---- Unfinished, please refer to the continued http://www.cnblogs.com/Brave-Heart/archive/2008/06/26/1230619.html

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.