当前位置: 代码迷 >> Java Web开发 >> 为何javascript写的如此飘逸,如此让人看不懂解决思路
  详细解决方案

为何javascript写的如此飘逸,如此让人看不懂解决思路

热度:271   发布时间:2016-04-17 10:25:10.0
为何javascript写的如此飘逸,如此让人看不懂
看一个树形代码,一个行政区划树,直接上代码
JScript code
function xmlPointer(data){    this.d=data;}xmlPointer.prototype={    text:function(){ if (!_isFF) return this.d.xml; var x = new XMLSerializer();   return x.serializeToString(this.d); },    get:function(name){return this.d.getAttribute(name); },    exists:function(){return !!this.d },    content:function(){return this.d.firstChild?this.d.firstChild.data:""; }, // <4k in FF    each:function(name,f,t,i){  var a=this.d.childNodes; var c=new xmlPointer(); if (a.length) for (i=i||0; i<a.length; i++) if (a[i].tagName==name) { c.d=a[i]; if(f.apply(t,[c,i])==-1) return; } },    get_all:function(){ var a={}; var b=this.d.attributes; for (var i=0; i<b.length; i++) a[b[i].name]=b[i].value; return a; },    sub:function(name){ var a=this.d.childNodes; var c=new xmlPointer(); if (a.length) for (var i=0; i<a.length; i++) if (a[i].tagName==name) { c.d=a[i]; return c; } },    up:function(name){ return new xmlPointer(this.d.parentNode);  },    set:function(name,val){ this.d.setAttribute(name,val);  },    clone:function(name){ return new xmlPointer(this.d); },    sub_exists:function(name){ var a=this.d.childNodes; if (a.length) for (var i=0; i<a.length; i++) if (a[i].tagName==name) return true;  return false;  },    through:function(name,rule,v,f,t){  var a=this.d.childNodes; if (a.length) for (var i=0; i<a.length; i++) { if (a[i].tagName==name && a[i].getAttribute(rule)!=null && a[i].getAttribute(rule)!="" &&  (!v || a[i].getAttribute(rule)==v )) { var c=new xmlPointer(a[i]);  f.apply(t,[c,i]); } var w=this.d; this.d=a[i]; this.through(name,rule,v,f,t); this.d=w;  } }}/***     @desc: tree constructor*     @param: htmlObject - parent html object or id of parent html object*     @param: width - tree width*     @param: height - tree height*     @param: rootId - id of virtual root node (same as tree node id attribute in xml)*     @type: public*     @topic: 0*/function dhtmlXTreeObject(htmlObject, width, height, rootId){    if (_isIE) try { document.execCommand("BackgroundImageCache", false, true); } catch (e){}    if (typeof(htmlObject)!="object")      this.parentObject=document.getElementById(htmlObject);    else      this.parentObject=htmlObject;    this.parentObject.style.overflow="hidden";       this._itim_dg=true;    this.dlmtr=",";    this.dropLower=false;    this.enableIEImageFix();   this.xmlstate=0;   this.mytype="tree";   this.smcheck=true;   //smart checkboxes   this.width=width;   this.height=height;   this.rootId=rootId;   this.childCalc=null;      this.def_img_x="18px";      this.def_img_y="18px";      this.def_line_img_x="18px";      this.def_line_img_y="18px";    this._dragged=new Array();   this._selected=new Array();   this.style_pointer="pointer";      this._aimgs=true;   this.htmlcA=" [";   this.htmlcB="]";   this.lWin=window;   this.cMenu=0;   this.mlitems=0;   this.iconURL="";   this.dadmode=0;   this.slowParse=false;   this.autoScroll=true;   this.hfMode=0;   this.nodeCut=new Array();   this.XMLsource=0;   this.XMLloadingWarning=0;   this._idpull={};   this._pullSize=0;   this.treeLinesOn=true;   this.tscheck=false;   this.timgen=true;   this.dpcpy=false;    this._ld_id=null;    this._oie_onXLE=[];   this.imPath=window.dhx_globalImgPath||"";    this.checkArray=new Array("iconUncheckAll.gif","iconCheckAll.gif","iconCheckGray.gif","iconUncheckDis.gif","iconCheckDis.gif","iconCheckDis.gif");   this.radioArray=new Array("radio_off.gif","radio_on.gif","radio_on.gif","radio_off.gif","radio_on.gif","radio_on.gif");   this.lineArray=new Array("line2.gif","line3.gif","line4.gif","blank.gif","blank.gif","line1.gif");   this.minusArray=new Array("minus2.gif","minus3.gif","minus4.gif","minus.gif","minus5.gif");   this.plusArray=new Array("plus2.gif","plus3.gif","plus4.gif","plus.gif","plus5.gif");   this.imageArray=new Array("leaf.gif","folderOpen.gif","folderClosed.gif");   this.cutImg= new Array(0,0,0);   this.cutImage="but_cut.gif";      dhtmlxEventable(this);   this.dragger= new dhtmlDragAndDropObject();//create root   this.htmlNode=new dhtmlXTreeItemObject(this.rootId,"",0,this);   this.htmlNode.htmlNode.childNodes[0].childNodes[0].style.display="none";   this.htmlNode.htmlNode.childNodes[0].childNodes[0].childNodes[0].className="hiddenRow";//init tree structures   this.allTree=this._createSelf();   this.allTree.appendChild(this.htmlNode.htmlNode);    if(_isFF){         this.allTree.childNodes[0].width="100%";         this.allTree.childNodes[0].style.overflow="hidden";    }   var self=this;   this.allTree.onselectstart=new Function("return false;");   if (_isMacOS)        this.allTree.oncontextmenu = function(e){             return self._doContClick(e||window.event, true);         };      this.allTree.onmousedown = function(e){ return self._doContClick(e||window.event); };        this.XMLLoader=new dtmlXMLLoaderObject(this._parseXMLTree,this,true,this.no_cashe);   if (_isIE) this.preventIECashing(true);        if (window.addEventListener) window.addEventListener("unload",function(){try{  self.destructor(); } catch(e){}},false);    if (window.attachEvent) window.attachEvent("onunload",function(){ try{ self.destructor(); } catch(e){}});    this.setImagesPath=this.setImagePath;    this.setIconsPath=this.setIconPath;    if (dhtmlx.image_path) this.setImagePath(dhtmlx.image_path);    if (dhtmlx.skin) this.setSkin(dhtmlx.skin);   return this;};
  相关解决方案