一个很好介绍js的例子

function UpdateInit(opt){
this.init(opt);
}

UpdateInit.prototype={
loadUrl:null,
loadParam:null,
beforeLoad:null,
afterLoad:null,
beforeInit:null,
afterInit:null,
customInit:null,
init:function(opt){
this.loadUrl=opt.loadUrl;
this.loadParam=opt.loadParam;
this.beforeLoad=opt.afterLoad;
this.afterLoad=opt.afterLoad;
this.beforeInit=opt.beforeInit
this.afterInit=opt.afterInit;
this.customInit=opt.customInit;
},

load:function(){
if(typeof(this.beforeLoad)=="function"){
this.beforeLoad(this);
}
var param={};
param.reqUrl=this.loadUrl;
param.reqData=this.loadParam;
if(typeof(this.customInit)=="function"){
ajaxRequest(param,this.customInit,this.loadError,null,this);
}
else{
ajaxRequest(param,this.initData,this.loadError,this,this);
}
},
initData:function(d){
if(!d){

return;
}

if(typeof(this.afterLoad)=="function"){
this.afterLoad();
}
if(typeof(this.beforeInit)=="function"){
this.beforeInit(d);
}
var itms=$(":input[type!='button']");
var $divInputer=null;
var tempVal=null;
for(var i=0,len=itms.length;i<len;i++){
tempVal=d[itms[i].name];
tempVal=tempVal!=null&&tempVal!="null"?tempVal:"";
if(typeof tempVal =="string"){
tempVal=tempVal.toString().replace(/(^s*)|(s*$)/g, "");
}

//保留几位小数
var dec=$(itms[i]).attr("decim");
if(tempVal===0||(dec!=null&&tempVal!=null&&tempVal!=""&&!isNaN(dec)&&!isNaN(tempVal)&&dec>0&&dec<20))
{
tempVal=tempVal.toFixed(dec);
}
$(itms[i]).val(tempVal);
$divInputer=$("#for_"+itms[i].name);
if($divInputer.length>0){
$divInputer.append(tempVal);
//$divInputer.append("").focus();
//$divInputer.html($divInputer.html());
}

}
if(typeof(this.afterInit)=="function"){
this.afterInit(d);
}
},

loadError:function(){
if(typeof(this.afterLoad)=="function"){
this.afterLoad();
}

}

};

原文地址:https://www.cnblogs.com/mengziHEHE/p/3331063.html