Skip to content

Instantly share code, notes, and snippets.

@small-carbon
Created July 26, 2013 06:34
Show Gist options
  • Select an option

  • Save small-carbon/6086749 to your computer and use it in GitHub Desktop.

Select an option

Save small-carbon/6086749 to your computer and use it in GitHub Desktop.
Ext 继承实现类源码分析
/**
* spirit from ext2.0
*/
_.extend = function() {
/**
* 覆盖实例成员
* @param E
* @constructor
*/
var C = function(E) {
for (var D in E) {
this[D] = E[D];
}
};
var e = Object.prototype.constructor;
// 声明构造属性
/**
* 继承,并由传递的值决定是否覆盖原对象的属性
* @param G superclass 父类 被继承
* @param O(该参数可选) 一个对象,将它本身携带的属性对子类进行覆盖
*/
return function(G, O) {
// 重新定义了函数 J = subclass 子类,用于继承(该类继承了父类所有属性,并最终返回该对象)
var J = function() {
G.apply(this, arguments);
}
var E = function() {
}, H, D = G.prototype;
// 父类属性
E.prototype = D;
// 赋值父类构造属性
// 下面两句就是上面最简单的继承实现
H = J.prototype = new E();
H.constructor = J;
// 添加了superclass属性指向父类的Prototype
J.superclass = D;
// J.superclass = G.prototype
if (D.constructor == e) {
D.constructor = G;
}
// 为[J]和[H]添加override函数
J.override = function(F) {
_.override(J, F);
// F对象被J对象重写
};
// H 是子类J的属性(例:H.superclass = J.prototype.supperclass = G.prototype )
// J.prototype.supperclass = G.prototype
H.superclass = H.supr = (function() {
return D;
});
// H.override = J.prototype.override
H.override = C;
// 覆盖掉子类prototype中的属性
_.override(J, O);
// 定义子类J的extend继承函数
J.extend = function(F) {
return _.extend(J, F)
};
return J;
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment