专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Javascript教程 » js解析xml:js继承 Base类的源码解析 »正文

js解析xml:js继承 Base类的源码解析

来源: 发布时间:星期三, 2008年12月31日 浏览:181次 评论:0
// timestamp: Tue, 01 May 2007 19:13:00
/*
base2.js - copyright 2007, Dean Edwards
http://www.opensource.org/licenses/mit-license
*/
// 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){
//参数大于个时
(arguments.length > 1) { // extending with a name/value pair
//获得proto祖先
var ancestor = this[source];
var value = arguments[1];
//如果value(第 2个参数)是function并且祖先对象存在在重载base时
(typeof value "function" && ancestor && /\bbase\b/.test(value)) {
// get the underlying method
var method = value;
// override
value = function{
var previous = this.base;
this.base = ancestor;
//上溯到父类对象
var Value = method.apply(this, arguments);
this.base = previous;
Value;
};
value.method = method;
value.ancestor = ancestor;
}
this[source] = value;
}

(source) { // extending with an object literal 用个对象列表来扩展
var extend = Base.prototype.extend;
/**
* 1.扩展原型思路方法和属性 2.
*/
//如果是扩展属于原型思路方法或属性,先遍历其重载Object3个思路方法
(Base._prototyping) {
var key, i = 0, members = ["constructor", "toString", "valueOf"];
while (key = members[i]) {
//如果是重载了这些思路方法
(source[key] != Object.prototype[key]) {
/**
* 逐个扩展,用call原因是要将extend上下文改为要扩展源this,
* 既是新建对象父类对象
*/
extend.call(this, key, source[key]);
}
}
}

(typeof this != "function") {
// 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)
(!Object.prototype[key]) {
extend.call(this, key, source[key]);
}
}
this;
},
base: Base
};
Base.extend = function(_instance, _){ // sub
/**
* Base类原型扩展别名,将这个当成个思路方法
*/
var extend = Base.prototype.extend;
/**
* build the prototype,创建原型
* 设置原型标志
*/
Base._prototyping = true;
/**
* 创建个Base例子,化继承部分
* 继承方式大致还是以下方式
* function A{}
* function B{
* this.b=;
* }
* A.prototype= B;//A继承B所有属性和思路方法
* 这种继承方式会有个问题,B中声明对象(如b)以prototype形式
* 被A继承的后,prototype只是生成个指向B中对象引用,即
* A所有例子会共同享有B中对象(b)
* var a1= A;
* var a2= A;
* a1.b.push("a11");
* a2.b.push("a21");
* 此时,a1.b=a2.b=["a11","a21"],
*
* Dean Edwards在实现继承时候,以父类为基础,创建例子,
* 利用extend扩展该例子,最后用A.prototype= B;实现继承
* 但是属性是对象时候没有做处理,
* 还是没有避开上述继承缺陷
*/
var proto= this;
/**
* 在这里,不可以用 proto.extend(_instance)代替
*/
extend.call(proto, _instance);
/**
* 类例子属性和思路方法原型部分构造完毕,删除标志位
*/
delete Base._prototyping;
/**
* 这里作者运用了适配器模式,用自定义构造器生成个新类对象
* wrapper/adapter:通过思路方法个对象封装或授权另
* 对象来改变它接口或者行为
*/
// create the wrapper for the constructor function
/**
* 获得构造器引用
*/
var constructor = proto.constructor;
/**
* 建立klassFunction对象,自定义构造器, klass就是衍生子类
* 两种情况下,此思路方法:
* 1.创建类例子时候,这时候不是原型构造阶段,执行由extend思路方法
* 继承时候设定构造思路方法
* 2.当用extend思路方法衍生子类时候--- this
* 下文中klass属性已经全部获得,
* 所以当完的后,获得所有父类思路方法和属性都包含在了
* proto里面了,这时候,在proto基础上运用prototypeextend思路方法
* 将此子类属性和思路方法添加到proto里面
*/
var klass = proto.constructor = function{
/**
* var proto= this; 父类构造,创建个父类例子
* this用完后,重定向到子类对象构造思路方法
*/
(!Base._prototyping) {
/**
* 当在构造中(constructor)base思路方法时,
* base思路方法会父类对象构造,这时候会嵌套
* 这个代码段,思路方法得以执行条件就是this._constructingtrue
*/
(this._constructing || this.constructor klass) { // instantiation
this._constructing = true;
constructor.apply(this, arguments);
delete this._constructing;
}
/**
*
* 不再向下执行
*/
{ // casting
var object = arguments[0];
(object != null) {
(object.extend || extend).call(object, proto);
}
object;
}
}
};
// build the erface
/**
*
*/
for (var i in Base){
klass[i] = this[i];
}
/**
* 创建继承链
*/
klass.ancestor = this;
klass.base = Base.base;
klass.prototype = proto;
klass.toString = this.toString;
/**
* 扩展类思路方法,属性,类似java
*/
extend.call(klass, _);
// initialisation 如果存在init
(typeof klass.init "function")
klass.init;
klass;
};
// initialise
Base = Base.extend({
constructor: function{
this.extend(arguments[0]);
}
}, {
ancestor: Object,
base: Base,
implement: function(_erface){
(typeof _erface "function") {
// it's a function, call it
_erface(this.prototype);
}
{
// add the erface using the extend method
this.prototype.extend(_erface);
}
this;
}
});
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: