// timestamp: Tue, 01 May 2007 19:13:00
/*
- copyright 2007, Dean Edwards
/licenses/mit-license
*/
// You know, writing a javascript library is awfully time consuming.
//////////////////// BEGIN: CLOSURE ////////////////////
// =========================================================================
// base2/
// =========================================================================
// version 1.1
var Base = function(){
// call this method from any other method to invoke that method's ancestor
};
= {
extend: function(source){
// When the parameter is greater than one
if ( > 1) { // extending with a name/value pair
//After the proto's ancestor
var ancestor = this[source];
var value = arguments[1];
//If value (second parameter) is a function and the ancestor object exists, when base is called in the overloaded function
if (typeof value == "function" && ancestor && /\bbase\b/.test(value)) {
// get the underlying method
var method = value;
// override
value = function(){
var previous = ;
= ancestor;
//Traced to the parent class object
var returnValue = (this, arguments);
= previous;
return returnValue;
};
= method;
= ancestor;
}
this[source] = value;
}
else
if (source) { // extending with an object literal
var extend = ;
/**
* 1. Extend prototype methods and properties 2.
*/
//If it is an extension method or property that belongs to the prototype, first iterate through the 3 methods of overloading the Object.
if (Base._prototyping) {
var key, i = 0, members = ["constructor", "toString", "valueOf"];
while (key = members[i++]) {
//If these methods are overloaded
if (source[key] != [key]) {
/**
* Expand one by one, the reason for using call is to change the context of extend to the source to be extended,
* It is the parent object of the newly created object
*/
(this, key, source[key]);
}
}
}
else
if (typeof this != "function") {
// if the object has a customised extend() method then use it
extend = || extend;
}
// copy each of the source object's properties to this object
for (key in source)
if (![key]) {
(this, key, source[key]);
}
}
return this;
},
base: Base
};
= function(_instance, _static){ // subclass
/**
* Extended alias for Base class prototype, treat this as a method call
*/
var extend = ;
/**
* build the prototype
* Set prototype flag
*/
Base._prototyping = true;
/**
* Create an instance of Base and initialize the inheritance part
* The inheritance method is roughly the following
* function A(){}
* function B(){
* =[];
* }
* =new B();//A inherits all attributes and methods of B
* This inheritance method will have a problem. The objects declared in B (such as b) are in the form of prototype
* After being inherited by A, prototype only generates a reference to the object in B, that is,
* All instances of A share the object in B (b)
* var a1=new A();
* var a2=new A();
* ("a11");
* ("a21");
* At this time, ==["a11","a21"],
*
* When Dean Edwards implements inheritance, creates instances based on the parent class,
* Use extend to extend this instance, and finally use =new B(); to achieve inheritance
* However, when the attribute is an object,
* Still haven't avoided the above inheritance defects
*/
var proto=new this;
/**
* Here, it cannot be replaced by (_instance)
*/
(proto, _instance);
/**
* The prototype part of the class instance attributes and methods has been constructed, and the flag bits are deleted.
*/
delete Base._prototyping;
/**
* Here the author uses the adapter pattern to generate a new class object using a custom constructor
* wrapper/adapter: One object encapsulates or authorizes another through certain methods
* Object to change its interface or behavior
*/
// create the wrapper for the constructor function
/**
* Get a reference to the constructor
*/
var constructor = ;
/**
* Create a Function object of klass and call a custom constructor. klass is a derived subclass
* In two cases, call this method:
* 1. When creating a class instance, it is not the prototype construction stage. Execute the extend method
* The construction method set during inheritance
* 2. When using extend method to derive subclasses ---new this
* Because the klass attributes have been obtained below,
* So after new, all methods and properties of the parent class are included
* Proto is inside, at this time, use the prototype extend method based on proto
* Add properties and methods of this subclass to the proto
*/
var klass = = function(){
/**
* var proto=new this; Call the constructor of the parent class and create an instance of the parent class
* After new this is used up, the function is redirected to the subclass object constructor
*/
if (!Base._prototyping) {
/**
* When the base method is called in the constructor,
* The base method will call the constructor of the parent class object, and will be nested at this time
* When calling this code segment, the condition for the method to be executed is this._constructing==true
*/
if (this._constructing || == klass) { // instantiation
this._constructing = true;
(this, arguments);
delete this._constructing;
}
/**
*
* No more downward execution
*/
else { // casting
var object = arguments[0];
if (object != null) {
( || extend).call(object, proto);
}
return object;
}
}
};
// build the class interface
/**
*
*/
for (var i in Base){
klass[i] = this[i];
}
/**
* Create an inheritance chain
*/
= this;
= ;
= proto;
= ;
/**
* Extended class methods, properties, static similar to java
*/
(klass, _static);
// class initialisation if the init function exists
if (typeof == "function")
();
return klass;
};
// initialise
Base = ({
constructor: function(){
(arguments[0]);
}
}, {
ancestor: Object,
base: Base,
implement: function(_interface){
if (typeof _interface == "function") {
// if it's a function, call it
_interface();
}
else {
// add the interface using the extend() method
(_interface);
}
return this;
}
});
/*
- copyright 2007, Dean Edwards
/licenses/mit-license
*/
// You know, writing a javascript library is awfully time consuming.
//////////////////// BEGIN: CLOSURE ////////////////////
// =========================================================================
// base2/
// =========================================================================
// version 1.1
var Base = function(){
// call this method from any other method to invoke that method's ancestor
};
= {
extend: function(source){
// When the parameter is greater than one
if ( > 1) { // extending with a name/value pair
//After the proto's ancestor
var ancestor = this[source];
var value = arguments[1];
//If value (second parameter) is a function and the ancestor object exists, when base is called in the overloaded function
if (typeof value == "function" && ancestor && /\bbase\b/.test(value)) {
// get the underlying method
var method = value;
// override
value = function(){
var previous = ;
= ancestor;
//Traced to the parent class object
var returnValue = (this, arguments);
= previous;
return returnValue;
};
= method;
= ancestor;
}
this[source] = value;
}
else
if (source) { // extending with an object literal
var extend = ;
/**
* 1. Extend prototype methods and properties 2.
*/
//If it is an extension method or property that belongs to the prototype, first iterate through the 3 methods of overloading the Object.
if (Base._prototyping) {
var key, i = 0, members = ["constructor", "toString", "valueOf"];
while (key = members[i++]) {
//If these methods are overloaded
if (source[key] != [key]) {
/**
* Expand one by one, the reason for using call is to change the context of extend to the source to be extended,
* It is the parent object of the newly created object
*/
(this, key, source[key]);
}
}
}
else
if (typeof this != "function") {
// if the object has a customised extend() method then use it
extend = || extend;
}
// copy each of the source object's properties to this object
for (key in source)
if (![key]) {
(this, key, source[key]);
}
}
return this;
},
base: Base
};
= function(_instance, _static){ // subclass
/**
* Extended alias for Base class prototype, treat this as a method call
*/
var extend = ;
/**
* build the prototype
* Set prototype flag
*/
Base._prototyping = true;
/**
* Create an instance of Base and initialize the inheritance part
* The inheritance method is roughly the following
* function A(){}
* function B(){
* =[];
* }
* =new B();//A inherits all attributes and methods of B
* This inheritance method will have a problem. The objects declared in B (such as b) are in the form of prototype
* After being inherited by A, prototype only generates a reference to the object in B, that is,
* All instances of A share the object in B (b)
* var a1=new A();
* var a2=new A();
* ("a11");
* ("a21");
* At this time, ==["a11","a21"],
*
* When Dean Edwards implements inheritance, creates instances based on the parent class,
* Use extend to extend this instance, and finally use =new B(); to achieve inheritance
* However, when the attribute is an object,
* Still haven't avoided the above inheritance defects
*/
var proto=new this;
/**
* Here, it cannot be replaced by (_instance)
*/
(proto, _instance);
/**
* The prototype part of the class instance attributes and methods has been constructed, and the flag bits are deleted.
*/
delete Base._prototyping;
/**
* Here the author uses the adapter pattern to generate a new class object using a custom constructor
* wrapper/adapter: One object encapsulates or authorizes another through certain methods
* Object to change its interface or behavior
*/
// create the wrapper for the constructor function
/**
* Get a reference to the constructor
*/
var constructor = ;
/**
* Create a Function object of klass and call a custom constructor. klass is a derived subclass
* In two cases, call this method:
* 1. When creating a class instance, it is not the prototype construction stage. Execute the extend method
* The construction method set during inheritance
* 2. When using extend method to derive subclasses ---new this
* Because the klass attributes have been obtained below,
* So after new, all methods and properties of the parent class are included
* Proto is inside, at this time, use the prototype extend method based on proto
* Add properties and methods of this subclass to the proto
*/
var klass = = function(){
/**
* var proto=new this; Call the constructor of the parent class and create an instance of the parent class
* After new this is used up, the function is redirected to the subclass object constructor
*/
if (!Base._prototyping) {
/**
* When the base method is called in the constructor,
* The base method will call the constructor of the parent class object, and will be nested at this time
* When calling this code segment, the condition for the method to be executed is this._constructing==true
*/
if (this._constructing || == klass) { // instantiation
this._constructing = true;
(this, arguments);
delete this._constructing;
}
/**
*
* No more downward execution
*/
else { // casting
var object = arguments[0];
if (object != null) {
( || extend).call(object, proto);
}
return object;
}
}
};
// build the class interface
/**
*
*/
for (var i in Base){
klass[i] = this[i];
}
/**
* Create an inheritance chain
*/
= this;
= ;
= proto;
= ;
/**
* Extended class methods, properties, static similar to java
*/
(klass, _static);
// class initialisation if the init function exists
if (typeof == "function")
();
return klass;
};
// initialise
Base = ({
constructor: function(){
(arguments[0]);
}
}, {
ancestor: Object,
base: Base,
implement: function(_interface){
if (typeof _interface == "function") {
// if it's a function, call it
_interface();
}
else {
// add the interface using the extend() method
(_interface);
}
return this;
}
});