// 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"