This file contains the openlayers. Class class, which is the base class of the entire openlayers framework. It is used to inherit from other child classes and implement multi-inheritance.
Create a new class and use the following syntax:
VaR Myclass = Openlayers. Class (prototype );
If multiple inheritance is performed, use the following:
VaR Myclass = Openlayers. Class (class1, class2, prototype );
Someone on the Internet did a good job of analysis.CodeAs follows:
Code
// Create a class, which is equivalent to defining a class in Java
Openlayers. Class = Function (){
// This class is returned as an anonymous function. The constructor calls the initalize () function.
VaR Class = Function (){
If (Arguments && Arguments [ 0 ] ! = Openlayers. Class. isprototype ){
This . Initialize. Apply ( This , Arguments );
}
};
// This is the object pointed to by class prototype.
VaR Extended = {};
// Parent is the parent class, and initialize is the initialize method of the parent class.
VaR Parent, initialize;
// Loop parameter list
For ( VaR I = 0 , Len = Arguments. length; I < Len; ++ I ){
If ( Typeof Arguments [I] = " Function " ){
// If the first parameter is of the function type, it indicates that the first parameter is of the parent class.
If (I = 0 && Len > 1 ){
// The following lines of code are quite similar. In fact, when the subclass inherits the parent class, it does not want to call the initialize method of the parent class. As long as other attributes of the parent class are inherited, the initialize method can overwrite and call the initialize method of the parent class.
Initialize = Arguments [I]. Prototype. initialize;
Arguments [I]. Prototype. initialize = Function (){};
Extended = New Arguments [I];
If (Initialize === Undefined ){
Delete Arguments [I]. Prototype. initialize;
} Else {
Arguments [I]. Prototype. initialize = Initialize;
}
}
// If it is a function type, but not the first parameter, the prototype object of the function is assigned to the parent.
Parent = Arguments [I]. Prototype;
} Else {
// This is an object. just assign a value.
Parent = Arguments [I];
}
// A shortest copy!
Openlayers. util. Extend (extended, parent );
}
// Point the prototype of the class function object to the extended object. In this way, the prototype of the class function object will automatically point to the extended object in the future.
Class. Prototype = Extended;
// Return this class function object, so that this function name will not be called class. What you start with is what the name is. This class has been defined since now!
Return Class;
};
// The following functions are out of date and are deleted in 3.0.
Openlayers. Class. isprototype = Function (){};
Openlayers. Class. Create = Function (){
Return Function (){
If (Arguments && Arguments [ 0 ] ! = Openlayers. Class. isprototype ){
This . Initialize. Apply ( This , Arguments );
}
};
};
Openlayers. Class. Inherit = Function (){
VaR Superclass = Arguments [ 0 ];
VaR PROTO = New Superclass (openlayers. Class. isprototype );
For ( VaR I = 1 , Len = Arguments. length; I < Len; I ++ ){
If ( Typeof Arguments [I] = " Function " ){
VaR Mixin = Arguments [I];
Arguments [I] = New Mixin (openlayers. Class. isprototype );
}
Openlayers. util. Extend (PROTO, arguments [I]);
}
Return PROTO;
};