Implementation of JavaScript Object Inheritance

Source: Internet
Author: User
Tags ibase

1. The following describes how to combine object impersonating (attributes) and prototype chain inheritance (objects):

function classa (scolor) {
This. color = scolor;
}< BR style = "line-Height: normal;">
classa. prototype. saycolor = function () {
alert (this. color);
};


function classb (scolor, sname) {
classa. call (this, scolor);
This. name = sname;
}< BR style = "line-Height: normal;">
classb. prototype = new classa ();
classb. prototype. sayname = function () {
alert (this. name);
}

// Test:
VaR B = new classb ("red", "sangkana ");
B. saycolor ();
B. sayname ();

2. Call zinherit library inheritance:

REFERENCE The zinherit. js file. The source code of the file is listed here:

Object. Prototype. inheritfrom = function (fnclass ){

Function inheritclasses (fnclass, arrclasses ){

Arrclasses. Push (fnclass );

If (typeof fnclass. _ superclasses _ = "object "){
For (VAR I = 0; I <fnclass. _ superclasses _. length; I ++ ){
Inheritclasses (fnclass. _ superclasses _ [I], arrclasses );
}
}
}

If (typeof this. constructor. _ superclasses _ = "undefined "){
This. constructor. _ superclasses _ = new array ();
}

Inheritclasses (fnclass, this. constructor. _ superclasses __);

For (prop in fnclass. Prototype ){
If (typeof fnclass. Prototype [prop] = "function "){
This [prop] = fnclass. Prototype [prop];
}
}
};

Object. Prototype. instanceof = function (func ){

If (this. constructor = func ){
Return true;
} Else if (typeof this. constructor. _ superclasses _ = "object "){
For (VAR I = 0; I <this. constructor. _ superclasses _. length; I ++ ){
If (this. constructor. _ superclasses _ [I] = func ){
Return true;
}
}
Return false;
} Else {
Return false;
}
};

Application:

Function classx (){
This. messagex = "this is the X message .";

If (typeof classx. _ initialized = "undefined "){

Classx. Prototype. saymessagex = function (){
Alert (this. messagex );
};

Classx. _ initialized = true;
}
}

Function classy (){
This. messagey = "this is the y message .";

If (typeof classy. _ initialized = "undefined "){

Classy. Prototype. saymessagey = function (){
Alert (this. messagey );
};

Classy. _ initialized = true;
}
}

Function classz (){
Classx. Apply (this );
Classy. Apply (this );
This. messagez = "this is the Z message .";

If (typeof classz. _ initialized = "undefined "){

Classz. Prototype. inheritfrom (classx );
Classz. Prototype. inheritfrom (classy );

Classz. Prototype. saymessagez = function (){
Alert (this. messagez );
};

Classz. _ initialized = true;
}
}

Test:

VaR objz = new classz ();
Objz. saymessagex ();
Objz. saymessagey ();
Objz. saymessagez ();

3. Application xbobjects Library:

<SCRIPT type = "text/JavaScript" src = "xbobjects. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">

_ Classes. registerclass ("polygon ");

Function polygon (isides ){

_ Classes. defineclass ("polygon", prototypefunction );

This. INIT (isides );

Function prototypefunction (){

Polygon. Prototype. init = function (isides ){
This. parentmethod ("init ");
This. sides = isides;
};

Polygon. Prototype. getarea = function (){
Return 0;
};

}
}

_ Classes. registerclass ("Triangle", "polygon ");

Function triangle (IBASE, iheight ){

_ Classes. defineclass ("Triangle", prototypefunction );

This. INIT (IBASE, iheight );

Function prototypefunction (){
Triangle. Prototype. init = function (IBASE, iheight ){
This. parentmethod ("init", 3 );
This. base = IBASE;
This. Height = iheight;
};

Triangle. Prototype. getarea = function (){
Return 0.5 * This. Base * This. height;
};
}

}

_ Classes. registerclass ("rectangle", "polygon ");

Function rectangle (ilength, iwidth ){

_ Classes. defineclass ("rectangle", prototypefunction );

This. INIT (ilength, iwidth );

Function prototypefunction (){
Rectangle. Prototype. init = function (ilength, iwidth ){
This. parentmethod ("init", 4 );
This. Length = ilength;
This. width = iwidth;
}

Rectangle. Prototype. getarea = function (){
Return this. length * This. width;
};

}
}

VaR triangle = New Triangle (12, 4 );
VaR rectangle = new rectangle (22, 10 );

Alert (triangle. sides );
Alert (triangle. getarea ());

Alert (rectangle. sides );
Alert (rectangle. getarea ());

</SCRIPT>

Note:

1. zinherit Database: http://www.nczonline.net/downloads

2. xbobjects Library: http://archive.bclary.com/xbProjects-docs/xbObject/

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.