網(LieHuo.Net)教程 我的JSKit 蠻早就開始寫了 一直都不滿意 也沒怎么用得上。在此其間 下載了眾多的框架 看他們的架構、設計 也沒有找到令自己滿意的版本 最終還是不斷的改進 其實就是一直在減少核心的代碼 到最終我比較滿意的版本 結果只封裝了兩個公共方法 一個是Import(載入代碼) 另一個是Run(執行代碼)。
為什么網上那么多優秀的 功能強大的框架我都不滿意?因為 我對框架的需求其實非常的簡單 我只需要它可以管理組織好我的代碼就OK了 我不需要在實現一個簡單的功能時 載入一堆在我實現某功能過程中 根本用不到的代碼 我很討厭這種浪費 所以尋覓了很久 都沒找到很中意的東西。
作為一名入門沒多久的程序,一直追求簡單的邏輯代碼,所以對于一些浪費是很看不慣的,對于一些復雜的邏輯 看了半天也看不懂,最后都是手寫了事。回顧這些年的編碼之路,可悲的發現,這輩子只能處于入門級別了,達不到更高的專家(師)級別了,因為學得太雜,精力被分散了。如果有來生,我想我會爭取一條道走到黑。
另外,再講兩句下面的這段代碼,因為代碼很少,所以也沒啥注釋可寫,只講一下思路吧。
Import方法:載入一個類,可以是一個很簡單的類,也可以是一個框架。 我的組織管理代碼的想法主要是通過import方法實現 比如我在jskit中建了一個 com.imcjj子文件夾,里面放了一個Browser.js 文件 就通過 JK.Import("com.imcjj/Browser") 導入Browser.js文件的代碼 "JK"是JSKit的一個實例
以下為引用的內容: /** * CJJ's JavaScript Kit * * @type : class * @version : v1.0 build 20090826 * @memo : none * ----------------------------------- * Copyright (c) cjj rusty_sun[AT]hotmail.com */ function JSKit(path){ if(!path){return null}; /*private member variable*/ var _runTimer=null, _importManage=[], _path=path; /*public method*/ JSKit.prototype.Import=function(file){ var script; var jskit=this; if(!_importManage[file]){_importManage[file]="loading"}; if(_importManage[file]!="loaded"){ script=document.createElement("script"); script.src =_path+file+".js"; script.onreadystatechange=function(){ if(this.readyState=='complete'){ var lastClass=file.substring(file.lastIndexOf("/")+1,file.length); if(lastClass=="Import"||lastClass=="Run"){return}; jskit[lastClass]=eval(lastClass); _importManage[file]="loaded"; script.onreadystatechange=null; return } }; script.onload=function(){ var lastClass=file.substring(file.lastIndexOf("/")+1,file.length); if(lastClass=="Import"||lastClass=="Run"){return}; jskit[lastClass]=eval(lastClass); _importManage[file]="loaded"; script.onload=null; }; document.getElementsByTagName("head")[0].appendChild(script); } };/*end of Import*/ JSKit.prototype.Run=function(code){ var loaded=true; var jskit=this; for(importFile in _importManage){ if(_importManage[importFile]=="loading"){loaded=false;break} }; if(!loaded){_runTimer=setTimeout(function(){jskit.Run(code)},500); return}; if(_runTimer){clearTimeout(_runTimer)}; if(typeof(code)=="function"){code()} };/*end of "Run"*/ } |
調用演示:
以下為引用的內容: var JK=new JSKit("/jskit/jskit.src/"); if(JK){ JK.Import("com.imcjj/FormValidate"); JK.Import("com.imcjj/Browser"); JK.Run(function(){ var browser=new JK.Browser(); alert(browser.name+""+browser.agent); var oFormValidate=new JK["FormValidate"]("frmDemo"); var oObj=oFormValidate._form; if(!oObj){alert("未發現表單,無法進行表單驗證");document.close();} var oTip=new Tip(); oTip._struct="<strong>{#text#}</strong>"; oTip._inertDirection=0; oTip.add("tip_Name", "tip_bottom", "自定義提示消息", oObj.elements["Name"].parentNode,"SPAN", "<strong>{#text#}</strong>"); //添加外部驗證函數 //oFormValidate.addValidate("isDate",fnCheckDate,"f","日期是否正確?"); //參數:驗證方法,需要驗證的表單元素的ID屬性 加*號表示該項為必填項 oFormValidate.addRule("isNotNull","*Card"); //oFormValidate.addRule("isDate","*Birthday"); //調用自定義驗證規則 //AJAX驗證最后一個是AJAX驗證函數參數列表:'getName.asp oFormValidate.addRule("isExists","Name",["'getName.asp'"], oTip); oFormValidate.addRule("isLen","*Password",[4,8]); oFormValidate.addRule("isSame","*Password1",["'password2'"]); oFormValidate.listen(); }); } |