JS , jQuery 汎用ルーチン定義環境メモ

疲れた

JS向け

(function(){

	var myUtil = {
		f : function(){},
		clone : function(o){
			this.f.prototype=o;
			return new this.f;
		},
		extend: function(obj,ext){
			for(var i in ext)obj[i]=ext[i];return obj;
		},
		classLoader : function(){
			return function(){
				var impl=arguments.callee.impl;
				var ins=myUtil.clone(impl)
				return impl.init?impl.init.apply(ins,arguments):impl
			}
		},
		uKey : function(){
			var d = new Date();
			return d.getFullYear()+'_'+d.getMonth()+'_'+d.getDate()+'_'+d.getHours()+'_'+d.getMinutes()+'_'+d.getSeconds()+'_'+d.getMilliseconds();
		}
	}
	
	var myJS = window.myJS=myUtil.classLoader();
	myJS.impl = myUtil.extend(myUtil.clone(myUtil),{
		init : function(){
			return this;
		}
	})
	
	var myClass = myJS.impl.myClass = myUtil.classLoader();
	myClass.impl = myUtil.extend(myJS(),{
		init : function(name){
			var o=this;
			o.name=name;
			o.stors=myJS.impl;
			o.stor=this.setConstructor();
			o.stor.__classObj__=o;
			return o.register();
		},
		register : function(){
			var o=this;
			return function(s,c){
				if(c)c.__super__=o.stors[s].impl;else c=s;
				o.stor.impl=c;
				return o.stor;
			}
		},
		setConstructor : function(){
			var o=this;
			return o.stors[o.name]=function(){
				return o.exeConstructor(o.getInstance(),arguments);
			}
		},
		getInstance : function(){
			var o=this;
			var ins=o.myClassLoader(o.stor.impl);
			return o.myClassLoader(o.stor.impl);
		},
		exeConstructor : function(ins,arg){
			var o=this;
			if(ins.init)return ins.init.apply(ins,arg)
			return ins;
		},
		myClassLoader : function(c){
			var o=this;
			if(c.__super__){
				var s=o.myClassLoader(c.__super__)
				return o.extend(o.extend(o.clone(s),c),{$super:s})
			}
			else return o.clone(c);
		}
	})

})()
(function($js){
    $js.MyClass('aaaaa')({
        init : function(){
            alert('init')       
            this.xxx=123
            return this;
        },
        test : function(){
            alert('test')
        },
        xxx : 0
    })
    $js.MyClass('bbbbb')('aaaaa',{
        init : function(){
            this.$super.init()
            alert(this.xxx)     
            return this;
        },
        test : function(){
            alert('testb')
        }
    })
    $js.bbbbb().test();
})(myJS())

jQuery向け

(function($j){

	var $js = myJS();

	$j.myJQ = function(f){
		if(!$j.fn.myJQ)$j.fn.myJQ = $j.myJQ.impl.myLoad
		var s=$j.myJQ.impl.mySelctor;
		s.$js=$js;
		if(f)$j(function(){f(s)})
		return s;
	}
	$j.myJQ.impl = {
		_myca : function(){
			var o=this;
			var ca = o.__$j__ = o.__$j__||{};
			ca.$j=ca.$j||o;
			ca.my$j=ca.my$j||{};
			ca.my$jGetter=ca.my$jGetter||{};
			ca.myMethod=ca.myMethod||{};
			return ca;
		},
		$idx : function(idx){
			var o=this;
			var ca=o._myca();
			if(idx==undefined || idx>=ca.$j.size())return ca.$j;
			var nw=ca.$j.eq(idx);
			nw.__$j__=ca;
			nw.my$j(ca.my$j)
			nw.myMethod(ca.myMethod)
			return nw;
		},
		myIdx : function(){
			if(this.size()>1)return undefined;
			return this.__$j__.$j.index(this[0])			
		},
		my$j : function(m){
			var o=this;
			var ca=o._myca();
			for (var i in m) {
				ca.my$j[i] = m[i];
				o[i] = ca.my$jGetter[i] = o.my$jGetter(m[i])
			}
			return this;
		},
		my$jGetter : function(o){
			return function(idx){
				return o.$idx(idx)
			}
		},
		myMethod : function(m){
			var o=this;
			var ca=o._myca();
			for (var i in m) {
				o[i] = ca.myMethod[i] = m[i];
			}
			return this;
		},
		mySelctor :	function( selector, context ) {
			return $j(selector,context).myJQ();
		},
		myLoad : function(){
			$j.extend(this,myJS.impl.clone($j.myJQ.impl));
			this.myOverRide('filter');
			this.myOverRide('eq');
			return this;
		},
		myOverRide : function(name){
			var o=this;
			var f=o[name];
			this[name]=function(){
				return $j.extend(f.apply(this,arguments),$js.clone($j.myJQ.impl));
			}
			return this;
		}
	}

	$j.myJQ().fn=$j.myJQ.impl

	$j.myJQ().fn.MyClass=function(){
		var ins=$js.clone(arguments.callee.impl)
		ins.$fn=this;
		return ins.init.apply(ins,arguments)
	}		
	$j.myJQ().fn.MyClass.impl={
		init : function(name){
			var o=this;			
			o.name=name;			
			return o.register()
		},
		register : function(){
			var o=this;			
			return function(s,c){
				var stor=$js.myClass(o.name)(s,c)
				var classObj=stor.__classObj__;
				o.$fn[o.name]=function(){
					return classObj.exeConstructor(o.active$jLoader(classObj.getInstance(),this),arguments);
				}
			}
		},
		active$jLoader:function(ins,n$j){
			var o=this;
			if(ins.$super){
				o.active$jLoader(ins.$super,n$j)
			}
			ins.$j=n$j
			return ins;
		}
	}

})(jQuery)
jQuery.myJQ(function($j){
    $j.fn.MyClass('uuu')({
        init :function(){
            alert('uuu '+this.$j.size())
        }
    })
    $j.fn.MyClass('ttt')('uuu',{
        init :function(){
            this.$super.init()
            alert('ttt '+this.$j.size())
        }
    })
    $j('body').ttt()
})