Skip to content

Instantly share code, notes, and snippets.

@Johnqing
Created May 9, 2014 04:09
Show Gist options
  • Save Johnqing/6c45bb029e9ed7e237a5 to your computer and use it in GitHub Desktop.
Save Johnqing/6c45bb029e9ed7e237a5 to your computer and use it in GitHub Desktop.
/**
* @namespace
* @desc 类控制器
*/
QHPAY.Class = (function(){
var _slice = [].slice;
function create(property){
if(!property.init){
property.init = function(){};
}
var _class = function(){
if(typeof this.init == 'function'){
this.init.apply(this, arguments);
}
};
var args = [];
args = _slice.call(arguments, 1);
//inherit
var key = null;
//父类的构造函数
var _super_init = null;
if(typeof args[0] == 'function'){
var parent = args[0];
_super_init = parent.prototype.init;
if(typeof _super_init == 'function'){
//销毁父实例的init;
parent.prototype.init = function(){};
var _this_init = property.init;
property.init=function(){
_super_init.apply(this, arguments);
_this_init.apply(this, arguments);
};
}
var iparent = new parent();
//复原
parent.prototype.init = _super_init;
//销毁父类
iparent.init = function(){};
//start为了防止父类函数执行的时候,this指针变化,特意把init拿出来执行
//end
_class.prototype = iparent;
_class.prototype.parent = parent;
_class.prototype._super = function(){
if(this.parent && this.parent.prototype[arguments[0]]){
return this.parent.prototype[arguments[0]].apply(this, _slice.call(arguments, 1));
}
};
args = args.slice(1);
}
//mixin
for ( var i = 0; i < args.length; i++) {
var arg = args[i];
if(typeof arg == 'object'){
for(key in arg){
_class.prototype[key] = arg[key];
}
}
}
//self
for(key in property){
_class.prototype[key] = property[key];
}
// if(!_class.prototype.init){
// _class.prototype.init=function(){};
// }
_class.prototype.constructor = _class;
return _class;
}
/**
* 单例
*/
function instance(className){
if(typeof className == 'string'){
className =eval(className);
}
if(typeof className ==='undefined') throw 'illegal class name';
if(typeof className._instance === 'undefined'){
className._instance = new className();
}
return className._instance;
}
return {
/**
*创建新类,可继承
*@param {object} property -类方法,包括init构造函数
*@param {function} parent -父类,继承对象
*@example
* //parentClass-<1>
*var Persion = Class.create({
* init:function(name){
* this.name = name;
* },
* getName:function(){
* return this.name;
* }
*});
* //parentClass-<2>
*var Person = function(name){
* this.name = name;
*}
*Person.prototype.getName = function(){
* return this.name;
*}
* //inherit<1> 继承构造类。
*var Man=Class.create({
* init:function(name,age){
* this.parent();
* this.parentClass.init.call(this,arguments);
* this.age = age;
* },
* getAge:function(){
* return this.age;
* }
*},Person);
* //inherit<2-1> 继承传统类第一种方式
*var Man1 = Class.create({
* getAge:function(){
* return this.age;
* }
*},Person);
* //inherit<2-2> 继承传统类第二种方式
*var Man2 = Class.create({
* init:function(name,age){
* this.parent();
* this.age = age;
* },
* getAge:function(){
* return this.age;
* }
*},Person);
*
*var Man3 = Class.create({
* init:function(){
*
* }
*},EventUtil,ENumberable);
*/
create: create,
/**
* 单例
* @param {class}
* @example
* Class.instance(Service);
*/
instance: instance
};
})();
/**
* 扩展静态方法
* @param {function/Class} -className/ModuleName/Object
* @param {object} -staticProperty -静态属性
* @example
* Class.extend(Dom,{
* create:function(nodeName){
* reutrn document.createElement(nodeName);
* }
* })
*/
QHPAY.Class.extend = function(className, staticProperty){
for(var key in staticProperty){
className[key] = staticProperty[key];
}
return className;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment