beauty("$Class",["$underscore"],function(_){
var Class = function () {
var length = arguments.length;
var option = arguments[length - 1];
option.init = option.init || function () {
};
// 如果参数中有要继承的父类
if (length === 2) {
/**
* @ignore
*/
var superClass = arguments[0].extend;
/**
* @ignore
*/
var tempClass = function () {
};
tempClass.prototype = superClass.prototype;
/**
* @ignore
*/
var subClass = function () {
this.init.apply(this, arguments);
};
// 加一个对父类原型引用的静态属性
subClass.superClass = superClass.prototype;
//subClass.superClass = superClass;
/**
* @ignore
*/
subClass.callSuper = function (context, func) {
var slice = Array.prototype.slice;
var a = slice.call(arguments, 2);
var func = subClass.superClass[func];
//var func = subClass.superClass.prototype[func];
if (func) {
func.apply(context, a.concat(slice.call(arguments)));
}
};
// 指定原型
subClass.prototype = new tempClass();
// 重新指定构造函数
subClass.prototype.constructor = subClass;
_.extend(subClass.prototype, option);
/**
* @ignore
*/
subClass.prototype.init = function () {
// 调用父类的构造函数
// subClass.superClass.init.apply(this, arguments);
// 调用此类自身的构造函数
option.init.apply(this, arguments);
};
return subClass;
// 如果参数中没有父类,则单纯构建一个类
} else {
if (length === 1) {
/**
* @ignore
*/
var newClass = function () {
// 加了return,否则init返回的对象不生效
return this.init.apply(this, arguments);
};
newClass.prototype = option;
return newClass;
}
}
};
beauty.$superPackage("$Class",Class);
});
原文:http://www.cnblogs.com/lhp2012/p/4809421.html