이 코드는 Ajax in Action 책에서 보았으며 이해할 수없는 두 가지가 있습니다 (웹 프로그래밍을 시작한 것을 기억하십시오. 여전히 자바 스크립트가 어떻게 작동하는지 이해하려고 노력함)."Ajax in Action"에서이 JavaScript 코드를 이해하는 데 도움을주십시오.
온라인 또는 378 페이지의 loadXMLDoc 함수에서 작성자가 "var loader = this;"라는 로컬 변수를 선언 한 이유는 무엇입니까? 그런 다음 "net.ContentLoader.onReadyState.call (loader);"호출에서 사용했습니다. 그냥 "net.ContentLoader.onReadyState.call (this);"대신 "
왜 저자가 "this.onReadyState();"대신 "net.ContentLoader.onReadyState.call (loader);"를 사용 했습니까?
/* url-loading object and a request queue built on top of it */ /* namespacing object */ var net=new Object(); net.READY_STATE_UNINITIALIZED=0; net.READY_STATE_LOADING=1; net.READY_STATE_LOADED=2; net.READY_STATE_INTERACTIVE=3; net.READY_STATE_COMPLETE=4; /*--- content loader object for cross-browser requests ---*/ net.ContentLoader=function(url,onload,onerror,method,params,contentType){ this.req=null; this.onload=onload; this.onerror=(onerror) ? onerror : this.defaultError; this.loadXMLDoc(url,method,params,contentType); } net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){ if (!method){ method="GET"; } if (!contentType && method=="POST"){ contentType='application/x-www-form-urlencoded'; } if (window.XMLHttpRequest){ this.req=new XMLHttpRequest(); } else if (window.ActiveXObject){ this.req=new ActiveXObject("Microsoft.XMLHTTP"); } if (this.req){ try{ var loader=this; this.req.onreadystatechange=function(){ net.ContentLoader.onReadyState.call(loader); } this.req.open(method,url,true); if (contentType){ this.req.setRequestHeader('Content-Type', contentType); } this.req.send(params); }catch (err){ this.onerror.call(this); } } } net.ContentLoader.onReadyState=function(){ var req=this.req; var ready=req.readyState; var httpStatus=req.status; if (ready==net.READY_STATE_COMPLETE){ if (httpStatus==200 || httpStatus==0){ this.onload.call(this); }else{ this.onerror.call(this); } } } net.ContentLoader.prototype.defaultError=function(){ alert("error fetching data!" +"\n\nreadyState:"+this.req.readyState +"\nstatus: "+this.req.status +"\nheaders: "+this.req.getAllResponseHeaders()); }
하지만 여기에서는 "net.ContentLoader.onReadyState.call (loader);" 로더는 try/catch 블록 내에서 this와 동일합니다. 즉 try 블록 이후에 확장 된 컨텍스트를 계속 가리 킵니다.try 블록 이전에 로더 변수 생성을 옮겨서는 안된다고 말하는 것에 대해? – nik
@NikhilRathod : 답변을 업데이트했습니다. 저자는 실제로'try/catch' 전에'this value'의 저장을 옮겨야합니다. 스코프 - 체인 룩업이 어쨌든이 방법을 해결할 것이기 때문에 여전히 여기에서 작동합니다. – jAndy
@jAndy : 왜 그는 익명의 기능을 만들었습니까? 무엇이 잘못되었는지 "this.req.onreadystatechange = net.ContentLoader.onReadyState.call (this);" – nik