Javascript class inheritance model

Source: Internet
Author: User

// Syntax:
Var object = // defines basic classes of lower-case objects for the most basic methods.
{
IsA: function (aType) // a basic method for judging the relationship between classes and between objects and Classes
{
Var self = this;
32
While (self)
{
If (self = aType)
Return true;
Self = self. Type;
};
Return false;
}
};
Function Class (aBaseClass, aClassDefine) // creates a Class function to declare the Class and its inheritance relationship.
{
Function class _ () // create a temporary function shell for the class
{
This. Type = aBaseClass; // we specify a Type attribute for each class and reference its inherited
Class
For (var member in aClassDefine)
This [member] = aClassDefine [member]; // define all the replication classes to the currently created
Class
};
Class _. prototype = aBaseClass;
Return new class _();
};
Function New (aClass, aParams) // creates an object function, used to create any Class Object
{
Function new _ () // create a temporary function shell for the object
{
This. Type = aClass; // We also specify a Type attribute for each object to access
To the class to which the object belongs
33
If (aClass. Create)
AClass. Create. apply (this, aParams); // we agree that the constructor of all classes is called Crea.
Te, which is similar to DELPHI
};
New _. prototype = aClass;
Return new _();
};
// Apply the following syntax:
Var Person = Class (object, // derived from the basic object Class
{
Create: function (name, age)
{
This. name = name;
This. age = age;
},
SayHello: function ()
{
Alert ("Hello, I'm" + this. name + "," + this. age + "years old .");
}
});
Var Employee = Class (Person, // derived to the Person Class, is it very similar to the general object language?
{
Create: function (name, age, salary)
{
Person. Create. call (this, name, age); // call the constructors of the base class
This. salary = salary;
},
ShowMeTheMoney: function ()
34
{
Alert (this. name + "$" + this. salary );
}
});
Var BillGates = New (Person, ["Bill Gates", 53]);
Var SteveJobs = New (Employee, ["Steve Jobs", 53,123 4]);
BillGates. SayHello ();
SteveJobs. SayHello ();
SteveJobs. ShowMeTheMoney ();
Var LittleBill = New (BillGates. Type, ["Little Bill", 6]); // create Based on the BillGate Type
LittleBill
LittleBill. SayHello ();
Alert (BillGates. isA (Person); // true
Alert (BillGates. isA (Employee); // false
Alert (SteveJobs. isA (Person); // true
Alert (Person. isA (Employee); // false
Alert (Employee. isA (Person); // true

 

This article is from mayi-hetu"
 

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.