关于javascript类继承的学习和发掘的代码实战class.js-处女篇1

//jQuery.Class
// It provides class level inheritance and callbacks.
// @author:mawei
// @date:2012/06/22
(function(Sharp)
{
// subClass can invoke it's superClass.Following by _prototype(prototype chain),
// it can invoke superClass or super-super-Class's any method, and superClass
// can invoke super-super-Class also;
var _super = function(mName)
{
var _methodFlag = this._methodFlag;
if(!this._methodFlag)this._methodFlag=[{method:mName,temp:null}];
else if(this._methodFlag[this._methodFlag.length-1].method != mName)this._methodFlag.push({method:mName,temp:this._methodFlag[this._methodFlag.length-1].temp});
var lastIndex = this._methodFlag.length-1;
var _prototype = this._methodFlag[lastIndex].temp || this._prototype;
var args = Array.prototype.slice.call(arguments, 1);

var ret;
while(_prototype)
{
var method = _prototype[mName];
if(method)
{
if(this._methodFlag[lastIndex].temp)
{
_prototype = _prototype._prototype;
this._methodFlag[lastIndex].temp = false;
continue;
}
if((!this._methodFlag[lastIndex].invoke && this[mName] == method) || this._methodFlag[lastIndex].invoke == method)
{
_prototype = _prototype._prototype;
continue;
}
this._methodFlag[lastIndex].temp = _prototype;
this._methodFlag[lastIndex].invoke = method;
ret = method.apply(this, args);
_prototype = null;
}
else _prototype = _prototype._prototype;
}
if(lastIndex == 0)_methodFlag == undefined ? delete this._methodFlag : this._methodFlag = _methodFlag;
else this._methodFlag.pop();
return ret;
}
// this is used in ajax callback or setTimeout and so on
var _callback = function(methodName)
{
var args = Array.prototype.slice.call(arguments,1);
return (function(object,methodName,params)
{
return function ()
{
var newParam = [].concat(params);
Sharp.iter(arguments, function(item)
{
newParam.push(item);
});
return object[methodName].apply(object,newParam);
}
})(this,methodName,args)
}

var _create = function(className,value)
{
if(typeof className == "string")
{
var names = className.split(".");
var context = window;
for(var i= 0,l=names.length;i<l;i++)
{
if(context)
{
if(!context[names[i]] && i != l-1)
{
context = context[names[i]] = {};
}
else if( i == l-1)
{
return context[names[i]] = value;
}
else if(context[names[i]])
{
context = context[names[i]];
}
}
}
}
}

var instanceOf = function(targetClass)
{
if(Sharp.isString(targetClass))
{
var _prototype = this;
var className = this.getClassName();
while(_prototype)
{
if(targetClass == className)
{
return true;
}
else
{
_prototype = _prototype._prototype;
if(_prototype && _prototype.getClassName)className = _prototype.getClassName();
}
}
}
else
{
var _prototype = this;
var classTemp = _prototype.clazz;
while(_prototype)
{
if(targetClass == classTemp)
{
return true;
}
else
{
_prototype = _prototype._prototype;
if(_prototype && _prototype.clazz)classTemp = _prototype.clazz;
}
}
}
return false;
}



Sharp.Class = function()
{
//initialize method
if(this.init)this.init.apply(this, arguments);
if(this.onInit)this.onInit.apply(this, arguments);
}
Sharp.extend(Sharp.Class, {
extend: function(className, classOption, objectOption)
{
/*var subClass = window[className] = function()
{
if(this.init)this.init.apply(this, arguments);
};*/
var _shutInit = false;

var subClass = _create(className,function()
{
if(!_shutInit)
{
if(this.init)this.init.apply(this, arguments);
if(this.onInit)this.onInit.apply(this, arguments);
}
})
Sharp.extend(subClass, Sharp.Class);
this._shutInit();
var prototype = subClass.prototype = new this;
this._shutInit();
if(arguments.length <= 2)
{
var objectOption = classOption || {};
var classOption = {};
}
for(var property in classOption)
{
if(':extend:'.indexOf(':' + property + ':') == -1)
{
subClass[property] = classOption[property];
}
}
subClass['_shutInit']=function(){_shutInit=!_shutInit};
for(var property in objectOption)
{
if(':_super:callback:_prototype:_shutInit:getClassName:instanceOf:clazz'.indexOf(':' + property + ':') == -1)
{
prototype[property] = objectOption[property];
}
}
var getClassName = function()
{
return this.clazz.className;
}
Sharp.extend(prototype,
{
_super:_super,
callback:_callback,
getClassName:getClassName,
instanceOf:instanceOf,
clazz:subClass
});
prototype.constructor = subClass;
subClass.className = className;
prototype._prototype = this.prototype;
},
_shutInit:function(){}
});

})(jQuery);

转载于:https://www.cnblogs.com/mawei1983/p/3448924.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值