// timestamp: Tue, 01 May 2007 19:13:00
// You know, writing a javascript library is awfully time consuming.
//////////////////// BEGIN: CLOSURE ////////////////////
// =========================================================================
// base2/base.js
// =========================================================================
// version 1.1
var base = function(){
// call this method from any other method to invoke that method's ancestor
};
base.prototype = {
extend: function(source){
//参数大于一个时
if (arguments.length > 1) { // extending with a name/value pair
//获得proto的祖先
var ancestor = this[source];
var value = arguments[1];
//如果value(第二个参数)是function,并且祖先对象存在,在重载函数中调用base时
if (typeof value == "function" && ancestor && /bbaseb/.test(value)) {
// get the underlying method
var method = value;
// override
value = function(){
var previous = this.base;
this.base = ancestor;
//上溯到父类对象
var returnValue = method.apply(this, arguments);
this.base = previous;
return returnValue;
};
value.method = method;
value.ancestor = ancestor;
}
this[source] = value;
}
else
if (source) { // extending with an object literal 用一个对象列表来扩展
var extend = base.prototype.extend;
//如果是扩展属于原型的方法或属性,先遍历其重载Object的3个方法
if (base._prototyping) {
var key, i = 0, members = ["constructor", "toString", "valueOf"];
while (key = members[i++]) {
//如果是重载了这些方法
if (source[key] != Object.prototype[key]) {
extend.call(this, key, source[key]);
}
}
}
else
if (typeof this != "function") {
// if the object has a customised extend() method then use it
extend = this.extend || extend;
}
// copy each of the source object's properties to this object
for (key in source)
if (!Object.prototype[key]) {
extend.call(this, key, source[key]);
}
}
return this;
},
base: base
};
base.extend = function(_instance, _static){ // subclass
var extend = base.prototype.extend;
base._prototyping = true;
var proto=new this;
extend.call(proto, _instance);
delete base._prototyping;
// create the wrapper for the constructor function
var constructor = proto.constructor;
var klass = proto.constructor = function(){
if (!base._prototyping) {
if (this._constructing || this.constructor == klass) { // instantiation
this._constructing = true;
constructor.apply(this, arguments);
delete this._constructing;
}
else { // casting
var object = arguments[0];
if (object != null) {
(object.extend || extend).call(object, proto);
}
return object;
}
}
};
// build the class interface
for (var i in base){
klass[i] = this[i];
}
klass.ancestor = this;
klass.base = base.base;
klass.prototype = proto;
klass.toString = this.toString;
extend.call(klass, _static);
// class initialisation 如果存在init函数 调用
if (typeof klass.init == "function")
klass.init();
return klass;
};
// initialise
base = base.extend({
constructor: function(){
this.extend(arguments[0]);
}
}, {
ancestor: Object,
base: base,
implement: function(_interface){
if (typeof _interface == "function") {
// if it's a function, call it
_interface(this.prototype);
}
else {
// add the interface using the extend() method
this.prototype.extend(_interface);
}
return this;
}
});



