jquery-class.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. (function ($){
  2. _Class=function(){
  3. var initializing=0,fnTest=/\b_super\b/,
  4. Class=function(){};
  5. Class.prototype={
  6. ops:function(o1,o2){
  7. o2=o2||{};
  8. for(var key in o1){
  9. this['_'+key]=key in o2?o2[key]:o1[key];
  10. }
  11. }
  12. };
  13. Class.extend=function(prop){
  14. var _super = this.prototype;
  15. initializing=1;//锁定初始化,阻止超类执行初始化
  16. var _prototype=new this();//只是通过此来继承,而非创建类
  17. initializing=0;//解锁初始化
  18. function fn(name, fn) {
  19. return function() {
  20. this._super = _super[name];//保存超类方法,此this后面通过apply改变成本体类引用
  21. var ret = fn.apply(this, arguments);//创建方法,并且改变this指向
  22. return ret;//返回刚才创建的方法
  23. };
  24. }
  25. var _mtd;//临时变量,存方法
  26. for (var name in prop){//遍历传进来的所有方法
  27. _mtd=prop[name];
  28. _prototype[name] =(typeof _mtd=='function'&&
  29. typeof _super[name]=='function'&&
  30. fnTest.test(_mtd))?fn(name,_mtd):_mtd;//假如传进来的是函数,进行是否调用超类的检测来决定是否保存超类
  31. }
  32. function F(arg1) {//构造函数,假如没有被初始化,并且有初始化函数,执行初始化
  33. if(this.constructor!=Object){
  34. return new F({
  35. FID:'JClassArguments',
  36. val:arguments
  37. });
  38. }
  39. if (!initializing&&this.init){
  40. if(arg1&&arg1.FID&&arg1.FID=='JClassArguments'){
  41. this.init.apply(this, arg1.val);
  42. }else{
  43. this.init.apply(this, arguments);
  44. }
  45. this.init=null;
  46. };
  47. }
  48. F.prototype=_prototype;//创建。。。
  49. F.constructor=F;//修正用
  50. F.extend=arguments.callee;
  51. return F;
  52. };
  53. return Class;
  54. }();
  55. $.Class=function(ops){
  56. return _Class.extend(typeof ops=='function'?ops():ops);
  57. };
  58. })(jQuery);