UNPKG

cejs

Version:

A JavaScript module framework that is simple to use.

484 lines 23.8 kB
�� /* ******** class template ******** (u =function(){}() <ONN�N�S ��� SWFObject 2.2 */ var ClassT={}; // class template set �� addCode _N�S�N��� // =================================================== /* ** class description: class template (simple version) _=this TODO: HISTORY: 2008/7/21 16:12:32 create */ var classT= (ClassT.classT=function(initF){ var // class private ----------------------------------- // instance constructor --------------------------- instanceL=[], initI=function(a){ var _t=this,_p=pv(_t); instanceL.push(_t); // for destructor //_p.vars=a // initial instance object },_=function(){initI.apply(this,arguments);initF&&initF.apply(this,arguments);}, // (instance private handle) N�� instance private �v�b��Qxe*R�csS�S0 _p='_'+(''+Math.random()).replace(/\./,''), // get private variables (instance[,destroy]), init private variables (instance[,access function list[, instance destructor]]) pv=function(i,d,k){var V,K=_p('k');return arguments.callee.caller===_p('i')?(V=_p(i[K]=_p()),V.O=i,V.L={}):(K in i)&&(V=_p(i[K]))&&i===V.O?d?(_p(i[K],1),delete i[K]):V.L:{};}; // class destructor --------------------------- /* please call at last (e.g., window.unload) usage: classT=classT.destroy(); or if you has something more to do: classT.destroy()&&classT=null; */ _.destroy=function(){for(var i=0,l=instanceL.length;i<l;i++)instanceL[i].destroy();_p();}; // (instance private handle, continue) eval('_p=(function(){var '+_p+'={a:pv,d:_.destroy,c:0,k:"+pv+'+Math.random()+'",i:initI};return function(i,d){var f=arguments.callee.caller;if(f==='+_p+'.a){if(!d)return i in '+_p+'?'+_p+'[i]:('+_p+'[i='+_p+'.c++]={},i);'+_p+'[i]={};}if(f==='+_p+'.d)'+_p+'={};}})();'); _p.toString=function(){return'';}; // class public interface --------------------------- // func _.func=function(){ var _t=this; }; // class constructor --------------------------- // do something others _.prototype={ // �ar��vϑ�bqg�>e(W class �instance \& � // ** important ** ��� N��\O object KN initialization �&TGR�V�p object �Sg copy reference ��Vdk new Bfqg�gN#j0initialization �_(W _() -N\O� // instance public interface ------------------- setV:function(a){ var _t=this,_p=pv(_t); //_p.vars=a }, // instance destructor --------------------------- /* usage: instance=instance.destroy(); or if you has something more to do: instance.destroy()&&instance=null; */ destroy:function(){pv(this,1);} }; // _.prototype= return _; })(); // function(initF){ // =================================================== // ㉐g // =================================================== /* ** class description: class template _=this TODO: .constructor -��[ inherits: http://www.cnblogs.com/birdshome/archive/2005/01/28/95933.html http://www.blogjava.net/zkjbeyond/archive/2006/05/08/45108.html HISTORY: 2008/7/21 16:12:32 create 7/24 15:14:16 enhanced by set .toString. But.. If we use delete _p.toString, than we still can.. eval('a = pv',some_instance); eval('var s=_p.toString;delete _p.toString;sl("** test1 _p: "+_p);_p.toString=s;sl("** test2 _p: "+_p);',a); 7/26 11:38:6 className TOTRY: �v�c \ eval NKb �O delete �c eval GR lost data */ var classT_debug= ///f N��� addCode _N�S�N��� � N�R ClassT.~ _N�lܕ�O0 (ClassT.classT=function(initF){ // initF: additional initialization function var // class private ----------------------------------- className='',//classT // variables vars=7, // function doLog=function(m){ var _t=_; sl((className?className+': ':'>> ')+m); // this+': ' }, // instance constructor --------------------------- instanceL=[], // �[�O list initI=function(a){ var _t=this,// ��b� g(u�v/f this � N/f arguments.callee0 _p=pv(_t);//pv(_t,[func1,func2,..],_.destroy); // init private variables instanceL.push(_t); // for destructor // initial instance object _t.property1=[]; _t.testV=a; doLog('class create: '+a); //return this; }, // �vϑn\ instance ԚMz& g(u�U� _=function(){ initI.apply(this,arguments); initF&&initF.apply(this,arguments); }, // (instance private handle) N�� instance private �v�b��Qxe*R�csS�S0 /* �f�[te�v2��l��k \ arguments.callee.caller �v z_�x ��x�[{v�N�Mbf}�NX[�S0 & (W eval() �v,{�N argument N!q(ufkKN0W0 */ _p='_'+(''+Math.random()).replace(/\./,''), pv=function(i,d,k){ // get private variables (instance[,destroy]), init private variables (instance[,access function list[, instance destructor]]) var V,K=_p('k'); /* V: get variables V.L variables {} V.O instance for full version: V.FSX[�S�Qxe�v z_�x list {} V.D instance destructor */ // simple version if(0)return arguments.callee.caller===_p('i')? (V=_p(i[K]=_p()),V.O=i,V.L={}) :(K in i)&&(V=_p(i[K]))&&i===V.O? d?(_p(i[K],1),delete i[K]):V.L :{}; // �z9e1\g�Q trouble // full version var c=arguments.callee.caller,j=0,l; doLog('pv: '+(c===_p('i')?'init':d?'destory':'get variables')+' by '+c); if(c===_p('i')){ V=_p(i[K]=_p()); V.O=i; if(k)V.D=k; // instance destructor // {v��SX[�S�Qxe�v z_�x0�f�r�v� N(u {} �(u d ��x�[/f *�[hQ�v T* �v function0 if(Array.isArray(d)) // []: no access!! for(k=V.F={},l=d.length;j<l;j++) k[d[j]]=1; return V.L={}; } // �2��z9e �~b \,g�O�ar��v index0 if(!(K in i)||!(V=_p(i[K]))||i!==V.O) for(k=_p('c');j<k;j++) if(i===_p(j).O){ doLog('pv: correct index: '+i[K]+' to '+j); i[K]=j; break; } if((K in i)&&(V=_p(i[K]))&&i===V.O) if(d){ // call from instance destructor if(!V.D||c===V.D)_p(i[K],1),delete i[K]; //else throw new Error(3,'Error calling to destroy.'); }else if(!V.F||V.F[c]){ doLog('pv: get index '+i[K]+'.'); //if(!V.L)throw new Error(1,'Error to get variables! Maybe it was already destroyed?');//private variables return V.L; } return {}; // throw new Error(1,'Illegal access!'); }; // of pv=function( // (for inherits) /* N�� inherit �v�b��k*R�csS�S0 usage: // !j�d inherits newClass=oldClass.clone(); // prevent re-use. 2�bk�Q � delete oldClass.clone; */ (_.clone=arguments.callee).toString=function(){return '[class_template]';}; // class destructor --------------------------- /* please call at last (e.g., window.unload) usage: classT=classT.destroy(); or if you has something more to do: classT.destroy()&&classT=null; */ _.destroy=function(){ doLog('destroy: Destroy class.'); for(var i=0,l=instanceL.length;i<l;i++) instanceL[i].destroy(); _p(); }; // class public interface --------------------------- // variables _.variables={}; // func _.func=function(){ var _t=this; }; // (instance private handle, continue) // .a: accessable caller function, .d: class destructor, c:count, .k: private variables keyword, .i: init function, m: accessable class members // destructor: (index,1) Warning: �g�N�t�X-N Array �S��!q�l(u delete? eval('_p=(function(){var '+_p+'={a:pv,d:_.destroy,c:0,k:"+pv+'+Math.random()+'",i:initI,m:{_:{}}}; (function(m,i){for(i=0;i<m.length;i++)if(m[i])'+_p+'.m[m[i]]=1;})([' //'func1,func2' // (W��kX N��� call ��υxe<P�v class private function0!q ��S�naTuO0R _.destroy MR0 +']); return function(i,d){var f=arguments.callee.caller;if(f==='+_p+'.a){if(!d)return i in '+_p+'?'+_p+'[i]:('+_p+'[i='+_p+'.c++]={},i);'+_p+'[i]={};}if('+_p+'.m[f])return '+_p+'.m._;if(f==='+_p+'.d)'+_p+'={};}})();'); _p.toString=function(){return '';}; // class constructor --------------------------- // do something others _.prototype={ // �ar��vϑ�bqg�>e(W class �instance \& � // instance public interface ------------------- // ** important ** ��� N��\O object KN initialization �&TGR�V�p object �Sg copy reference ��Vdk new Bfqg�gN#j0initialization �_(W _() -N\O� property1:[], setV:function(m){ var _t=this,_p=pv(_t); doLog('setV: class vars='+(vars=m)+', this.vars='+(_p.vars=m)); }, getV:function(){ var _t=this,_p=pv(_t); doLog('getV: class vars='+vars+', this.vars='+_p.vars); }, // instance destructor --------------------------- /* usage: instance=instance.destroy(); or if you has something more to do: instance.destroy()&&instance=null; TODO: Need *auto* detect if the object is destroyed. */ destroy:function(){ // If you need to do something (e.g, destroy sub-sub-objects) before destroy it, you need to call pv(this) first here. pv(this,1); } }; // _.prototype= // class name --------------------------- if(className){ _.prototype.toString=_.toString=function(){return '[class '+className+']';};//classT //eval('var className=_',this); // L� N� } return _; })(); // (function(){ // =================================================== /* // test suit sl('<hr />'); var c1=new classT(123),c2=new classT(456); c1.setV('Hello'); c2.setV('World'); c1.getV(); c2.getV(); sl(c1.testV+','+c2.testV); c1.destroy(); c2.destroy(); classT.destroy(); */ /* |~b http://www.cnblogs.com/birdshome/archive/2005/01/28/95933.html _eQarguments.length���Nh��PeQ_xe Pxe�w��^ � �arguments.callee.length���Nh�(function)ꁫ��[��v_xe Pxe�w��^ � function fn(){ var _f=arguments.callee; // f: function, flag _eQarguments.callee���Nh�(function)ꁫ� //N _f.* �f�N fn.* alert('fn: _f.val='+_f.val); } fn.subfn=function(){ var _f=arguments.callee; // function, flag // vu N(u new Bf ��N _f.* �f�N fn.subfn.* �this.* �f�N fn.*0FO(W (fn.subfn=function(){ .. }) -N��1\ N��(u this, _f alert('fn.subfn: this.val='+this.val); // *** !!WARNING!! (W eval() -N this �S��h�:y window ��Bf1\!q�l(u this �O�_0R fn �N0 alert('fn.subfn: _f.val='+_f.val); }; fn.subfn.val='test value: fn.subfn.val'; fn.val='test value: fn.val'; fn.prototype={ constructor:fn, // need to adjust subfn:function(){ var _f=arguments.callee; // function, flag // vu N(u new Bf ��N _f.* �f�N fn.subfn.* �this.* �f�N fn.* alert('fn.prototype.subfn: this.val='+this.val); alert('fn.prototype.subfn: _f.val='+_f.val); alert('fn.prototype.subfn: this.constructor.val='+this.constructor.val); }, val:'test value: fn.prototype.val' }; // fn.prototype={ fn.prototype.subfn.val='test value: fn.prototype.subfn.val'; var inst=new fn; //inst.constructor.subfn(); inst.subfn(); var obj={ val:34, fn:function(){ var _f=arguments.callee; // function, flag //N _f.* �f�N obj.subfn.* �this.* �f�N obj.* alert(this.val); } }; ww.ee(); to hack @ Firefox 3.0: https://bugzilla.mozilla.org/show_bug.cgi?id=442333 GP-� scope obj.fn � gN P foo ��xe �(W Firefox N�S�NO(u var a; eval('a = foo', obj.fn) �b foo ��xe�b0R a �b� �@b�N`O�Y�g gO(u�.z�e�l�O>e��[hQ'` gܕ�vqg� �1\�_͑�e�j�gN!k z_�x�Module Pattern Provides No Privacy& at least not in JavaScript(TM)0(WNc��֊�_ �� P�R��Sb�{(W Firefox 3.1 �b�c�Remove eval's optional second argument0 */ /* �[\Oclass: http://www.remus.dti.ne.jp/~a-satomi/bunsyorou/ArekorePopup.html http://www.klstudio.com/post/32.html function classA(){ // define property // array this.attrs=['title','href','cite','datetime']; // object this.ns={ xhtml1:'http://www.w3.org/1999/xhtml', xhtml2:'http://www.w3.org/2002/06/xhtml2', }; return this; } classA.prototype={ // function launch:function(){}, init:function(){}, // object scanNode:{ recursive:function(node){} }, // http://dean.edwards.name/weblog/2005/10/add-event/ addEvent:function(obj,type,listener){ if (obj.addEventListener) // Std DOM Events obj.addEventListener(type, listener, false); else if (obj.attachEvent) // IE obj.attachEvent( 'on' + type, function() { listener( { type : window.event.type, target : window.event.srcElement, currentTarget : obj, clientX : window.event.clientX, clientY : window.event.clientY, pageY : document.body.scrollTop + window.event.clientY, keyCode : window.event?window.event.keyCode:e?e.which:0, shiftKey : window.event.shiftKey, stopPropagation : function() { window.event.cancelBubble=true; } } ) } ); } }; var A=new classA(); */