首页 » 编写高质量代码:改善JavaScript程序的188个建议 » 编写高质量代码:改善JavaScript程序的188个建议全文在线阅读

《编写高质量代码:改善JavaScript程序的188个建议》寤鸿139锛氫娇鐢╯cript瀹炵幇寮傛閫氫俊

关灯直达底部

鍒╃敤锛渟cript锛炴爣绛捐兘澶熷姩鎬佸姞杞藉閮↗avaScript鑴氭湰鏂囦欢銆侸avaScript鑴氭湰鏂囦欢涓嶄粎鍙互琚墽琛岋紝杩樺彲浠ヤ紶杈撴暟鎹紝鎵€浠ヤ笉濡ㄥ湪鏈嶅姟鍣ㄧ浣跨敤JavaScript鏂囦欢鏉ラ檮鍔犱紶閫掑搷搴斾俊鎭紝褰撳湪瀹㈡埛绔娇鐢╯cript鍏冪礌鍔犺浇杩滅▼鑴氭湰鏂囦欢鏃讹紝杩欎簺闄勫姞鍦↗avaScript鏂囦欢涓殑鍝嶅簲淇℃伅涔熶竴鍚岃鍔犺浇鍒板鎴风锛岃嚜鐒朵篃鑳藉杈惧埌寮傛淇℃伅浜や簰鐨勭洰鐨勩€?/p>

渚嬪锛屾柊寤轰竴涓鎴风淇℃伅浜や簰鍜岃姹傞〉闈紙script_main.htm锛夛紝鐒跺悗杈撳叆涓嬮潰鐨勪唬鐮侊細


锛渉tml锛?/p>

锛渉ead锛?/p>

锛渢itle锛炲紓姝ヤ俊鎭氦浜掞紲/title锛?/p>

锛渟cript language=/"javascript/"type=/"text/javascript/"锛?/p>

function callback(info){

alert(info);

}

锛?script锛?/p>

锛渟cript type=/"text/javascript/"src=/"script_serve.js/"锛烇紲/script锛?/p>

锛?head锛?/p>

锛渂ody锛?/p>

锛渉1锛炲鎴风淇℃伅浜や簰椤甸潰锛?h1锛?/p>

锛?body锛?/p>

锛?html锛?/p>


鎺ョ潃鍦ㄦ湇鍔$鐨剆cript_serve.js鑴氭湰鏂囦欢涓皟鐢ㄥ洖璋冨嚱鏁癱allback锛屼唬鐮佸涓嬶細


callback(/"Hi锛屽ぇ瀹跺ソ锛屾垜鏄粠鏈嶅姟鍣ㄧ杩囨潵鐨勪俊鎭娇鑰呫€?);


姝ゆ椂锛屽鏋滆繍琛屽鎴风浜や簰椤甸潰锛坰cript_main.htm锛夛紝灏嗕細鍦ㄥ鎴风浜や簰椤甸潰涓脊鍑衡€淗i锛屽ぇ瀹跺ソ锛屾垜鏄粠鏈嶅姟鍣ㄧ杩囨潵鐨勪俊鎭娇鑰呫€傗€濈殑鍝嶅簲淇℃伅锛岃繖浜涗俊鎭潵鑷湇鍔″櫒绔€?/p>

褰撴湇鍔″櫒绔疛avaScript鏂囦欢琚姞杞藉埌瀹㈡埛绔氦浜掗〉闈腑鏃讹紝瀹冨寘鍚殑鑴氭湰灏辨垚涓轰氦浜掗〉闈綔鐢ㄥ煙涓殑涓€閮ㄥ垎锛屼篃灏辨槸璇达紝script_serve.js鏂囦欢涓殑鑴氭湰瀹為檯涓婂凡缁忔垚涓簊cript_main.htm椤甸潰鑴氭湰鐨勪竴閮ㄥ垎锛屽嵆鏈€缁堢殑杩愯缁撴灉搴旇鏄繖鏍风殑锛?/p>


锛渉tml锛?/p>

锛渉ead锛?/p>

锛渢itle锛炲紓姝ヤ俊鎭氦浜掞紲/title锛?/p>

锛渟cript language=/"javascript/"type=/"text/javascript/"锛?/p>

function callback(info){

alert(info);

}

锛?script锛?/p>

锛渟cript language=/"javascript/"type=/"text/javascript/"锛?/p>

//鏈嶅姟鍣ㄧ鍝嶅簲椤甸潰

callback(/"Hi锛屽ぇ瀹跺ソ锛屾垜鏄粠鏈嶅姟鍣ㄧ杩囨潵鐨勪俊鎭娇鑰?/");

锛?script锛?/p>

锛?head锛?/p>

锛渂ody锛?/p>

锛渉1锛炲鎴风淇℃伅浜や簰椤甸潰锛?h1锛?/p>

锛?body锛?/p>

锛?html锛?/p>


褰撶劧涔熷彲浠ュ姩鎬佺敓鎴恠cript鍏冪礌锛屼互瀹炵幇閫氳繃script鍏冪礌瀹炴柦寮傛浜や簰鍔熻兘鐨勫皝瑁呫€?/p>

绗?姝ワ紝鍦ㄩ〉闈腑瀹氫箟涓€涓紓姝ヨ姹傜殑灏佽鍑芥暟锛屼唬鐮佸涓嬶細


//鍒涘缓锛渟cript锛炴爣绛?/p>

//鍙傛暟锛歎RL琛ㄧず瑕佽姹傜殑鏈嶅姟鍣ㄧ鏂囦欢璺緞

//杩斿洖鍊硷細鏃?/p>

function request(url){

if(!document.script){

document.script=document.createElement(/"script/");

document.script.setAttribute(/"type/",/"text/javascript/");

document.script.setAttribute(/"src/",url);

document.body.appendChild(document.script);

}

else{

document.script.setAttribute(/"src/",url);

}

}


绗?姝ワ紝瀹屽杽瀹㈡埛绔氦浜掗〉闈㈢殑缁撴瀯鍜岃剼鏈唬鐮併€備笂闈㈣繖涓姹傚嚱鏁版槸鏁翠釜script寮傛浜や簰鐨勬牳蹇冦€備笅闈㈠氨鍙互鏉ヨ璁″鎴风浜や簰椤甸潰锛屼唬鐮佸涓嬶細


锛渉tml锛?/p>

锛渉ead锛?/p>

锛渢itle锛炲紓姝ヤ俊鎭氦浜掞紲/title锛?/p>

锛渟cript language=/"javascript/"type=/"text/javascript/"锛?/p>

function callback(info){//瀹㈡埛绔洖璋冨嚱鏁?/p>

alert(info);

}

function request(url){//script寮傛璇锋眰鍑芥暟

//浠g爜鍚屼笂

}

window.onload=function{//椤甸潰鍒濆鍖栧鐞嗗嚱鏁?/p>

var b=document.getElementsByTagName(/"input/")[0];

b.onclick=function{//涓洪〉闈㈡寜閽粦瀹氬紓姝ヨ姹傚嚱鏁?/p>

request(/"script_serve.js/");

}

}

锛?script锛?/p>

锛?head锛?/p>

锛渂ody锛?/p>

锛渉1锛炲鎴风淇℃伅浜や簰椤甸潰锛?h1锛?/p>

锛渋nput name=/"submit/"type=/"button/"id=/"submit/"/锛?/p>

锛?body锛?/p>

锛?html锛?/p>


绗?姝ワ紝鍦ㄦ湇鍔″櫒绔殑鍝嶅簲鏂囦欢锛坰cript_serve.js锛変腑杈撳叆涓嬮潰鐨勪唬鐮侊細


callback(/"Hi锛屽ぇ瀹跺ソ锛屾垜鏄粠鏈嶅姟鍣ㄧ杩囨潵鐨勪俊鎭娇鑰呫€?);


涓婇潰script_serve.js鏂囦欢鏄疛avaScript鑴氭湰鏂囦欢锛岃€屼笉鏄叾浠栫被鍨嬬殑缃戦〉鏂囦欢锛岃繖鏍峰綋鍗曞嚮鎸夐挳鏃舵墠浼氳Е鍙戝紓姝ヨ姹傚拰鍝嶅簲琛屼负銆?/p>

褰撲娇鐢╯cript鍏冪礌浣滀负寮傛閫氫俊鐨勫伐鍏锋椂锛屽疄鐜颁俊鎭氦鎹㈢殑鏈€绠€鍗曟柟娉曞氨鏄埄鐢ㄥ弬鏁颁粠瀹㈡埛绔悜鏈嶅姟鍣ㄧ浼犻€掍俊鎭紝杩欑鍦║RL涓檮鍔犲弬鏁扮殑鏂瑰紡鏄渶蹇嵎鐨勬柟娉曪紙URL涓檮鍔犲弬鏁板嵆鏌ヨ瀛楃涓诧級銆傜劧鍚庢湇鍔″櫒绔帴鏀惰繖浜涘弬鏁帮紝骞舵妸鍝嶅簲淇℃伅浠avaScript鑴氭湰褰㈠紡浼犲洖瀹㈡埛绔€?/p>

渚嬪锛屽湪瀹㈡埛绔氦浜掗〉闈紙濡傗€渟cript寮傛閫氫俊涔嬪弬鏁颁紶閫抇main.htm鈥濓級涓互涓嬮潰鐨勫舰寮忓悜鏈嶅姟鍣ㄥ彂鍑鸿姹傦細


锛渉tml锛?/p>

锛渉ead锛?/p>

锛渢itle锛炲紓姝ヤ俊鎭氦浜掞紲/title锛?/p>

锛渟cript language=/"javascript/"type=/"text/javascript/"src=

/"code_serve.js?id=8/"锛烇紲/script锛?/p>

锛渂ody锛?/p>

锛渉1锛炲鎴风淇℃伅浜や簰椤甸潰锛?h1锛?/p>

锛?body锛?/p>

锛?html锛?/p>


鍦↗avaScript澶栭儴鏂囦欢鐨刄RL涓檮鍔犱簡涓€涓弬鏁癷d=8锛岃繖涓弬鏁版槸瀹㈡埛绔紶閫掔粰鏈嶅姟鍣ㄧ鐨勶紝甯屾湜鏈嶅姟鍣ㄨ兘澶熸帴鏀惰鍙傛暟锛屽苟鑳藉鏍规嵁璇ュ弬鏁板搷搴旂浉搴旂殑淇℃伅锛屼紶鍥炲搷搴斾俊鎭€傝繖绉嶆兂娉曟槸濂界殑锛岄棶棰樻槸璇ュ浣曡鏈嶅姟鍣ㄧ鑾峰彇杩欎釜鍙傛暟淇℃伅锛堝id=8锛夊憿锛?/p>

鍒╃敤Location瀵硅薄鐨剆earch灞炴€ц兘澶熸崟鑾稨TTP鐨刄RL浠ユ煡璇㈠瓧绗︿覆淇℃伅锛屽嵆鍦ㄦ湇鍔″櫒绔殑code_serve.js鏂囦欢涓緭鍏ヤ笅闈㈢殑浠g爜锛?/p>


var queryString=location.search.substring(1);

alert(queryString);


浣嗘槸锛屽綋杩愯瀹㈡埛绔氦浜掗〉闈㈡椂锛屾彁绀轰俊鎭负绌猴紝璇存槑鏈嶅姟鍣ㄧ骞舵病鏈夋帴鏀跺埌杩欎釜鍙傛暟锛屾槸涓嶆槸寰堝鎬紵濡傛灉浣跨敤涓嬮潰鐨勪唬鐮佹帴鏀禜TTP涓畬鏁寸殑URL瀛楃涓蹭俊鎭紝鍒欎細杩斿洖瀹㈡埛绔氦浜掗〉鐮佺殑URL瀛楃涓诧紝鑰屼笉鏄摼鎺ョ殑JavaScript鏂囦欢URL锛堝鈥渉ttp://localhost/mysite/script寮傛閫氫俊涔嬪弬鏁颁紶閫抇main.htm鈥濆瓧绗︿覆锛夈€傞毦鎬娇鐢ㄤ笂闈唬鐮佷細鎺ユ敹涓嶅埌浠讳綍鍙傛暟鍛紒


var queryString=location.href;

alert(queryString);


浣跨敤Location瀵硅薄鏄笉鑳藉鎺ユ敹瀹㈡埛绔氦浜掗〉闈腑鍖呭惈鐨勫閮↗avaScript杩炴帴鏂囦欢鐨刄RL瀛楃涓蹭俊鎭殑銆傚湪鏈嶅姟鍣ㄧJavaScript鏂囦欢涓紝浣跨敤鑴氭湰鏉ヨ鍙栧鎴风浜や簰椤甸潰涓紲script锛炴爣绛剧殑src灞炴€у€硷紝浠g爜濡備笅锛?/p>


/*閬嶅巻瀹㈡埛绔氦浜掗〉闈㈢殑鎵€鏈夛紲script锛炴爣绛撅紝鎵惧埌src灞炴€у寘鍚?script寮傛閫氫俊涔嬪弬鏁颁紶閫抇serve.js/"鐨勬爣绛撅紝骞跺尮閰嶅嚭鏉ヨURL鐨勫弬鏁帮紝浠庝腑绛涢€夊嚭闄勫甫鍥炶皟鍑芥暟鍚嶇О鐨勫弬鏁帮紝鐒跺悗鍒╃敤杩欎釜鍥炶皟鍑芥暟鎵ц鏈嶅姟鍣ㄧ浼犻€掔殑淇℃伅*/

var js=/"script寮傛閫氫俊涔嬪弬鏁颁紶閫抇serve.js/";//鍖归厤鐨凧avaScript鏂囦欢鍚嶇О

var r=new RegExp(js+/"(//?(.*))?$/");//瀹氫箟鍖归厤鍙傛暟鐨勬鍒欒〃杈惧紡

var script=document.getElementsByTagName(/"script/");/*鑾峰彇瀹㈡埛绔氦浜掗〉闈腑鍖呭惈鐨勬墍鏈塻cript鍏冪礌*/

for(var i=0;i锛渟cript.length;i++){//閬嶅巻鎵€鏈塻cript鍏冪礌

var s=script[i];

if(s.src锛嗭紗s.src.match(r)){//鍒ゆ柇鏄惁瀛樺湪鍙傛暟

var oo=s.src.match(r)[2];

if(oo锛嗭紗(t=oo.match(/([^锛?]+)=([^=锛哴+)/g))){//鍖归厤鍑烘墍鏈夊弬鏁?/p>

for(var l=0;l锛渢.length;l++){//閬嶅巻鎵€鏈夊弬鏁?/p>

r=t[l];

var c=r.match(/([^锛?]+)=([^=锛哴+)/);//鍖归厤姣忎釜鍙傛暟

if(c锛嗭紗(c[2]==/"callback/")){

var f=eval(c[2]);//婵€娲诲洖璋冨嚱鏁板悕绉板瓧绗︿覆

f(/"Hi锛屽ぇ瀹跺ソ锛屾垜鏄粠鏈嶅姟鍣ㄧ杩囨潵鐨勪俊鎭娇鑰呫€?);

}

}

}

}

}


涓婇潰鐨凧avaScript鏂囦欢鏄湇鍔″櫒绔姹傜殑鑴氭湰鏂囦欢銆傛帴涓嬫潵杩愯瀹㈡埛绔氦浜掗〉闈紙script寮傛閫氫俊涔嬪弬鏁颁紶閫抇main_js.htm锛夛紝鍦ㄥ崟鍑诲叾涓殑銆愯姹傘€戞寜閽箣鍚庯紝浼氬脊鍑烘纭俊鎭€?/p>

瑕佷娇鐢↗avaScript鏂囦欢鍚戞湇鍔″櫒璇锋眰淇℃伅锛屽苟甯屾湜寰楀埌鏈嶅姟鍣ㄧ殑绉瀬鍝嶅簲锛屽彲浠ユ妸script鍏冪礌鐨剆rc灞炴€ц缃负璇锋眰鏈嶅姟鍣ㄧ鑴氭湰鏂囦欢锛岃€屼笉鏄疛avaScript鏂囦欢銆備緥濡傦紝浠SP鏈嶅姟鍣ㄦ妧鏈负渚嬶紝鍙互杩欐牱杩涜璇锋眰锛?/p>


window.onload=function{

var b=document.getElementsByTagName(/"input/")[0];

b.onclick=function{

var url=/"script寮傛閫氫俊涔嬪弬鏁颁紶閫抇serve.asp?callback=callback/";//璇锋眰ASP鏂囦欢

request(url);

}

}


鍗宠姹傜殑鏄枃浠跺悕涓篈SP绫诲瀷鐨勬湇鍔″櫒绔剼鏈枃浠讹紝鑰屼笉鏄疛avaScript鑴氭湰鏂囦欢銆傝繖鏍凤紝灏卞彲浠ュ埄鐢ㄦ湇鍔″櫒鎶€鏈潵鎺ユ敹璇锋眰浼犻€掔殑鍙傛暟浜嗭紝浠g爜濡備笅锛堝弬瑙佲€渟cript寮傛閫氫俊涔嬪弬鏁颁紶閫抇serve.asp鈥濇枃浠讹級锛?/p>


锛[email protected]=/"VBSCRIPT/"CODEPAGE=/"65001/"%锛?/p>

锛?

callback=Request.QueryString(/"callback/")

Response.Write(/"callback(/'Hi锛屽ぇ瀹跺ソ锛屾垜鏄粠鏈嶅姟鍣ㄧ杩囨潵鐨勪俊鎭娇鑰呫€?)/")

%锛?/p>


鍙︿竴涓渶瑕佽鑰呮敞鎰忕殑闂灏辨槸瀛楃缂栫爜鐨勪竴鑷存€с€傚綋鏈嶅姟鍣ㄥ悜瀹㈡埛绔搷搴斾俊鎭椂锛屽湪HTTP浼犺緭涓墍浣跨敤鐨勫瓧绗︾紪鐮侀粯璁や负UTF-8锛屽嵆鍥介檯閫氱敤缂栫爜銆傚鏋滄湇鍔″櫒绔剼鏈紪鐮佷负涓枃绠€浣擄紝鍒欏簲璇ュ湪鏈嶅姟鍣ㄧ鍝嶅簲淇℃伅鐨勫ご閮ㄥ畾涔変俊鎭殑缂栫爜涓篏B2312銆備緥濡傦紝鍦ˋSP鑴氭湰鏂囦欢涓彲浠ヨ繖鏍疯缃細


锛[email protected]=/"VBSCRIPT/"CODEPAGE=/"936/"%锛?/p>

锛?

callback=Request.QueryString(/"callback/")

Response.AddHeader/"Content-Type/",/"text/html;charset=gb2312/"

Response.Write(/"callback(/'Hi锛屽ぇ瀹跺ソ锛屾垜鏄粠鏈嶅姟鍣ㄧ杩囨潵鐨勪俊鎭娇鑰呫€?)/")

%锛?/p>


鍦ㄥ鎴风浜や簰椤甸潰涓簲璇ヨ缃〉闈㈢紪鐮侊紝鍏蜂綋缂栫爜涓庢湇鍔″櫒绔姹傞〉闈㈢殑缂栫爜绫讳技锛?/p>


锛渕eta http-equiv=/"Content-Type/"content=/"text/html;charset=utf-8/"锛?/p>


瑕佺‘淇濆湪寮傛浜や簰杩囩▼涓笉鍙戠敓涔辩爜鐜拌薄锛屽簲璇ヤ繚璇佺浉鍏抽〉闈㈠拰浠g爜鐨勫瓧绗︾紪鐮佹槸涓€鑷寸殑锛屾棦鍙互缁熶竴浣跨敤鍥介檯閫氱敤缂栫爜锛屼篃鍙互缁熶竴浣跨敤涓枃绠€浣撶紪鐮侊紙936鎴朑B2312锛夛紝榛樿浣跨敤鍥介檯閫氱敤缂栫爜锛堝嵆65001鎴朥TF-8锛夈€傝櫧鐒讹紲script锛炴爣绛緎rc灞炴€ц姹傜殑鏄疉SP鏂囦欢锛屼絾鏄疉SP鍝嶅簲鐨勫瓧绗︿覆鏄鍚圝avaScript璇硶瑙勫垯鐨勫瓧绗︿覆锛屽綋杩欎簺瀛楃涓茶鍔犺浇鍒板鎴风鐨勶紲script锛炴爣绛惧唴閮ㄦ椂锛屽氨浼氳杞崲涓哄彲浠ユ墽琛岀殑JavaScript鑴氭湰浠g爜銆?/p>

鏈€鍚庯紝涓轰簡甯姪璇昏€呬簡瑙d竴涓畬鏁磋€屽張娓呮櫚鐨剆cript寮傛閫氫俊涓弬鏁颁紶閫掔殑杩囩▼锛屾垜浠妸瀹㈡埛绔拰鏈嶅姟鍣ㄧ瀵瑰簲鐨勬枃浠朵唬鐮佸叏閮ㄦ暣鐞嗗嚭鏉ワ紙閬靛惊榛樿鐨勫浗闄呴€氱敤缂栫爜锛夈€傚鎴风浜や簰椤甸潰鐨勫畬鏁翠唬鐮侊紙html鏂囦欢绫诲瀷锛夊涓嬶細


锛渉tml锛?/p>

锛渉ead锛?/p>

锛渢itle锛炲紓姝ヤ俊鎭氦浜掞紲/title锛?/p>

锛渟cript language=/"javascript/"type=/"text/javascript/"锛?/p>

function callback(info){//鍥炶皟鍑芥暟

alert(info);

}

function request(url){//璇锋眰鍑芥暟

if(!document.script){

document.script=document.createElement(/"script/");

document.script.setAttribute(/"type/",/"text/javascript/");

document.script.setAttribute(/"src/",url);

document.body.appendChild(document.script);

}else{

document.script.setAttribute(/"src/",url);

}

}

window.onload=function{//椤甸潰鍒濆鍖栧鐞?/p>

var b=document.getElementsByTagName(/"input/")[0];

b.onclick=function{//榧犳爣鍗曞嚮浜嬩欢澶勭悊鍑芥暟锛屼紶閫掕姹傜殑鏈嶅姟鍣ㄧ鑴氭湰URL鍜屽弬鏁?/p>

var url=/"script寮傛閫氫俊涔嬪弬鏁颁紶閫抇serve.asp?callback=callback/"

request(url);

}

}

锛?script锛?/p>

锛渕eta http-equiv=/"Content-Type/"content=/"text/html;

charset=utf-8/"锛烇紲/head锛?/p>

锛渂ody锛?/p>

锛渉1锛炲鎴风淇℃伅浜や簰椤甸潰锛?h1锛?/p>

锛渋nput name=/"submit/"type=/"button/"id=/"submit/"/锛?/p>

锛?body锛?/p>

锛?html锛?/p>


鏈嶅姟鍣ㄧ鍝嶅簲椤甸潰鐨勫畬鏁翠唬鐮侊紙ASP鏂囦欢绫诲瀷锛夊涓嬶細


锛[email protected]=/"VBSCRIPT/"CODEPAGE=/"65001/"%锛?/p>

锛?

callback=Request.QueryString(/"callback/")/'鎺ユ敹鍙傛暟

Response.Write(/"callback(/'Hi锛屽ぇ瀹跺ソ锛屾垜鏄粠鏈嶅姟鍣ㄧ杩囨潵鐨勪俊鎭娇鑰呫€?)/")/'杈撳嚭鍝嶅簲淇℃伅

%锛?/p>


鍦ㄦ祴璇曚笂闈唬鐮佹椂锛屽簲纭繚鍦ㄦ湇鍔″櫒鐜涓嬭繍琛岋紝鍚﹀垯杈句笉鍒伴鏈熺粨鏋溿€?/p>