Berikut ialah fungsi kawalan utama dan beberapa kaedah awam. Termasuk pengecualian melontar dan pembalakan. Kembangkan, lintasi. Tetapkan laluan model kaedah. Dapatkan model (mesti ditetapkan sebelum anda boleh mendapatkannya, jika tidak pengecualian akan dilemparkan) untuk menilai (data).
Pembetulan
(function(window,undefined){ var_toString = {}.toString, _is, _writeScript, _listUrl, _scriptMap = { method_url : {},//method --> url <method : url ><1 : 1 > url_method : {},//url --> method <url : [method,method] ><1 : n > visited : {}, writted : {}, wait_write:[] }, _readyFnx = [], alfred = function(){//自己单独使用也可以。象jquery alfred.ready.apply(window,arguments); }; //私有函数开始 _is = function(it,type){//复杂的可以自己进行设置。正则匹配。 returntype.test(_toString.call(it).toLowerCase()); }; _waitReady = function(){ //等结束。如果结束了。直接跳出循环。其实就是利用JS的这个特性。 if(document.readyState != "loaded" &&document.readyState != "complete") { vartime = setTimeout(arguments.callee,0); return; } clearTimeout(time); } _checkReady = function(){ //处理参数 for(vari=0;i<arguments.length;i++) { _readyFnx.push(arguments[i]); } varcallback = _readyFnx.shift(); //支持高级事件的 if(document.addEventListener) { document.addEventListener("DOMContentLoaded",function(){ _waitReady(); document.removeEventListener("DOMContentLoaded",arguments.callee,false); callback.apply(window,_readyFnx); }); returnthis; } //支持事件则用事件处理 IE if(document.attachEvent) { document.attachEvent( "onreadystatechange", function(){ _waitReady(); document.detachEvent( "onreadystatechange",arguments.callee); callback.apply(window,_readyFnx); }); returnthis; } //不支持的则看不在 iframe 下时候的特殊属性。看 JSCRIPT MSDN。 if(document.documentElement.doScroll &&window == window.top) { try { document.documentElement.doScroll("left","top"); } catch(e) { setTimeout(arguments.callee, 0); return;} callback.apply(window,_readyFnx); returnthis; } }; _listUrl = function(){ varl = _scriptMap.visited,i; //对我们的URL 进行筛选 。主要防止多次添加访问过的依赖库。 for(i inl){ if(!_scriptMap.writted[i]) { _scriptMap.wait_write.push(i); } } _scriptMap.wait_write.reverse(); _writeScript(); }; _writeScript = function(){ //写入我们的JS 了。 varhead = document.getElementsByTagName("head").item(0),len = _scriptMap.wait_write.length; for(vari;_scriptMap.wait_write.length&&(i=_scriptMap.wait_write.shift());) { varscript = document.createElement("script"), url = i || ""; script.setAttribute("async",true);//高级浏览器支持HTML5特性 script.setAttribute("type","text/javascript"); script.setAttribute("src",url); head.appendChild(script); _scriptMap.writted[url] = true;//这里无所谓,只要有这个URL就可以 }; }; alfred.extend = function(){ vardeep = false, len, options, name, src, copy, copyIsArray, clone, target = arguments[0] || {}, i = 1; len = arguments.length; if(!len) { returnthis; } if(alfred.isBoolean(target)) { deep = arguments[0]; to = arguments[1]; i = 2; } if(!(alfred.isObject(target) || alfred.isFunction(target))) { target = {}; } if(len = i) { target = this; --i; } for(;i<len;i++) { if((options=arguments[i])!==null) { for(name inoptions) { copy = options[name]; src = target[name]; if(src){throw"Becareful : you are rewrite the "+_handle+"."+name+"!"} if(copy===target) {continue} if(deep &© &&(alfred.isObject(copy) || (copyIsArray = alfred.isArray(copy)))) { if(copyIsArray) { copyIsArray = false; clone = src &&alfred.isArray(src) ? src : []; } else { clone = src &&alfred.isObject(src) ? src : []; } target[ name ] = alfred.extend( deep, clone, copy ); } elseif( copy !== undefined ) { target[name] = copy; } } } } }; alfred.isNumber = function(it){return_is(it,/^[object number]$/)}; alfred.isString = function(it){return_is(it,/^[object string]$/)}; alfred.isFunction = function(it){return_is(it,/^[object function]$/)}; alfred.isObject = function(it){return_is(it,/^[object object]$/)}; alfred.isArray = function(it){return_is(it,/^[object array]$/)}; alfred.isBoolean = function(it){returntypeofit ==='boolean'}; alfred.isDom = function(it){return_is(it,/^[object htmlw+]$/)}; alfred.extend({ author : "alfred", version : 1.01, global : window, doc : window.document, reset : function(it){alfred.global[it]=alfred;}, log : function() { if(window.console &&console.log) { returnconsole.log.apply(window,arguments) } }, error : function(name,value) { throw{"name":name,"message":value}; }, require : function(method) { varm2u = _scriptMap.method_url, u2m = _scriptMap.url_method, r; if(!(m2u[method])){throw"Error : you need setMethod first"} if(!_scriptMap.visited[m2u[method][0]] &&!_scriptMap.writted[m2u[method][0]]) { //haven't be visited or write _scriptMap.visited[m2u[method][0]] = true; //use an beautiful code in here for(; m2u[method][1].length &&(r=m2u[method][1].shift());) { alfred.require(r); } _listUrl(); } }, setMethod : function(url,method,rely) { vari, method = alfred.isArray(method)? method:[method], rely = alfred.isArray(rely)?rely:[rely]; if(url &&!_scriptMap.url_method[url]){ _scriptMap.url_method[url] = method; for(;method.length&&(i=method.shift());) { _scriptMap.method_url[i] = {"0" : url, "1" : rely}; } returnthis; } throw"Error : you are setMethod in an old method"; }, ready : function(){ _checkReady.apply(window,arguments); }, each : function(arg,callback){ for(vari inarg){ if(arg[i]) { callback.call(arg[i],i); } } returnarg; } }); window.alfred = alfred; })(window)
Perkenalkan fail ini dahulu apabila menggunakannya. Kemudian anda perlu menetapkan
alfred.setMethod("basic.js",["alfred","alfred.global","alfred.doc","alfred.reset","alfred.log","alfred.isNumber", "alfred.isFunction","alfred.isArray","alfred.isObject","alfred.isString","alfred.isBoolean", "alfred.require","alfred.setMethod","alfred.extend"],[]);
seperti ini Selepas menetapkannya, anda boleh memberitahu JS berikut bahawa saya telah menyediakan kaedah ini. Hanya taip alfred.require("alfred.reset"); untuk menarik fail ke dalam halaman dan mendaftarkannya.
Beginilah rupa halaman tersebut.
<!DOCTYPE html> <html> <head> <title> </title> </head> <body> <script type="text/javascript" src="base.js"></script> <script type="text/javascript" src="deps.js"></script> <script type="text/javascript"> alfred.reset("$"); $.require("alfred.dom"); $(function(){ vara = $.dom("<div id='test' style='width:200px;height:200px;background:red;'> adf</div>"); $.dom("body").prepend(a); }) </script> </body> </html>
alfred.reset adalah untuk kemudahan menetapkan nama kaedah. Dengan cara ini kita boleh menggunakannya hampir seperti jquery.
alfred.reset("$");
$.require("alfred.dom");
$.dom("div a span:first-child ").append("hello alfred");
boleh digunakan seperti ini.
Atas ialah kandungan terperinci Bagaimana untuk menetapkan fungsi utama java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!