鎴戜粠鏉ヤ笉鐞嗚ВJavaScript闂寘锛岀洿鍒版湁浜鸿繖鏍峰悜鎴戣В閲婂畠

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎴戜粠鏉ヤ笉鐞嗚ВJavaScript闂寘锛岀洿鍒版湁浜鸿繖鏍峰悜鎴戣В閲婂畠相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/const' title='const'>const   cat   璇存槑   fine   鎯呭喌   ddn   閮ㄥ垎   澶嶆潅   鐩稿姞   

褰撲唬鐮佸湪JavaScript涓繍琛屾椂锛屾墽琛屼唬鐮佺殑鐜闈炲父閲嶈锛屽苟灏嗘鎷负浠ヤ笅鍑犵偣锛?br/>鍏ㄥ眬浣滅敤鍩熲€斺€旂涓€娆℃墽琛屼唬鐮佺殑榛樿鐜銆?br/>鍑芥暟浣滅敤鍩熲€斺€斿綋鎵ц娴佽繘鍏ュ嚱鏁颁綋鏃躲€?/p>

(鈥? 鈥斺€?鎴戜滑褰撲綔 鎵ц涓婁笅鏂?鏄綋鍓嶄唬鐮佹墽琛岀殑涓€涓幆澧冧笌浣滅敤鍩熴€?/p>

鎹㈠彞璇濊锛屽綋鎴戜滑鍚姩绋嬪簭鏃讹紝鎴戜滑浠庡叏灞€鎵ц涓婁笅鏂囦腑寮€濮嬨€備竴浜涘彉閲忔槸鍦ㄥ叏灞€鎵ц涓婁笅鏂囦腑澹版槑鐨勩€傛垜浠О涔嬩负鍏ㄥ眬鍙橀噺銆傚綋绋嬪簭璋冪敤涓€涓嚱鏁版椂锛屼細鍙戠敓浠€涔?

浠ヤ笅鍑犱釜姝ラ锛?/h1>
  1. JavaScript鍒涘缓涓€涓柊鐨勬墽琛屼笂涓嬫枃锛屾垜浠彨浣滄湰鍦版墽琛屼笂涓嬫枃銆?/li>
  2. 杩欎釜鏈湴鎵ц涓婁笅鏂囧皢鏈夊畠鑷繁鐨勪竴缁勫彉閲忥紝杩欎簺鍙橀噺灏嗘槸杩欎釜鎵ц涓婁笅鏂囩殑鏈?鍦板彉閲忋€?/li>
  3. 鏂扮殑鎵ц涓婁笅鏂囪鎺ㄥ埌鍒版墽琛屽爢鏍堜腑銆傚彲浠ュ皢鎵ц鍫嗘爤鐪嬩綔鏄竴绉嶄繚瀛樼▼搴忓湪鍏舵墽琛屼腑鐨勪綅缃殑瀹瑰櫒銆?/li>

鍑芥暟浠€涔堟椂鍊欑粨鏉?褰撳畠閬囧埌涓€涓猺eturn璇彞鎴栦竴涓粨鏉熸嫭鍙穧銆?/p>

褰撲竴涓嚱鏁扮粨鏉熸椂锛屼細鍙戠敓浠ヤ笅鎯呭喌:

  1. 杩欎釜鏈湴鎵ц涓婁笅鏂囦粠鎵ц鍫嗘爤涓脊鍑恒€?/li>
  1. 鍑芥暟灏嗚繑鍥炲€艰繑鍥炶皟鐢ㄤ笂涓嬫枃銆傝皟鐢ㄤ笂涓嬫枃鏄皟鐢ㄨ繖涓湰鍦扮殑鎵ц涓婁笅鏂囷紝瀹冨彲浠ユ槸鍏ㄥ眬鎵ц涓婁笅鏂囷紝涔熷彲浠ユ槸鍙﹀涓€涓湰鍦扮殑鎵ц涓婁笅鏂囥€傝繖鍙栧喅浜庤皟鐢ㄦ墽琛屼笂涓嬫枃鏉ュ鐞嗘鏃剁殑杩斿洖鍊硷紝杩斿洖鐨勫€煎彲浠ユ槸涓€涓璞°€佷竴涓暟缁勩€佷竴涓嚱鏁般€佷竴涓竷灏斿€肩瓑绛夛紝濡傛灉鍑芥暟娌℃湁return璇彞锛屽垯杩斿洖undefined銆?/p>

  2. 杩欎釜鏈湴鎵ц涓婁笅鏂囪閿€姣侊紝閿€姣佹槸寰堥噸瑕侊紝杩欎釜鏈湴鎵ц涓婁笅鏂囦腑澹版槑鐨勬墍鏈夊彉閲忛兘灏嗚鍒犻櫎锛屼笉鍦ㄦ湁鍙橀噺锛岃繖涓氨鏄负浠€涔?绉颁负鏈湴鎵ц涓婁笅鏂囦腑鑷湁鐨勫彉閲忋€?/li>

鍩虹鐨勪緥瀛?/h1>

鍦ㄨ璁洪棴鍖呬箣鍓嶏紝璁╂垜浠湅涓€涓嬩笅闈㈢殑浠g爜锛?/p>

1: let a = 3
2: function addTwo(x) 
3:   let ret = x + 2
4:   return ret
5: 
6: let b = addTwo(a)
7: console.log(b)

涓轰簡鐞嗚Вjavascript寮曟搸鏄浣曞伐浣滅殑锛岃鎴戜滑璇︾粏鍒嗘瀽涓€涓嬶細

鍦ㄧ1琛岋紝鎴戜滑鍦ㄥ叏灞€鎵ц涓婁笅鏂囦腑澹版槑浜嗕竴涓柊鍙橀噺a锛屽苟灏嗚祴鍊间负3銆?/p>

鎺ヤ笅鏉ュ氨鍙樺緱妫樻墜浜嗭紝绗?琛屽埌绗?琛屽疄闄呬笂鏄湪涓€璧风殑銆傝繖閲屽彂鐢熶簡浠€涔? 鎴戜滑鍦ㄥ叏灞€鎵ц涓婁笅鏂囦腑澹版槑浜嗕竴涓悕涓篴ddTwo鐨勬柊鍙橀噺,鎴戜滑缁欏畠鍒嗛厤浜嗕粈涔?涓€涓嚱鏁板畾涔夈€備袱涓嫭鍙穥涔嬮棿鐨勪换浣曞唴瀹归兘琚垎閰嶇粰addTwo锛屽嚱鏁板唴閮ㄧ殑浠g爜娌℃湁琚眰鍊硷紝娌℃湁琚墽琛岋紝鍙槸瀛樺偍鍦ㄤ竴涓彉閲忎腑浠ュ灏嗘潵浣跨敤銆?/p>

鐜板湪鎴戜滑鍦ㄧ6琛屻€傚畠鐪嬭捣鏉ュ緢绠€鍗曪紝浣嗘槸杩欓噷鏈夊緢澶氫笢瑗块渶瑕佹媶寮€鍒嗘瀽銆傞鍏堬紝鎴戜滑鍦ㄥ叏灞€鎵ц涓婁笅鏂囦腑澹版槑涓€涓柊鍙橀噺锛屽苟灏嗗叾鏍囪涓篵锛屽彉閲忎竴缁忓0鏄庯紝鍏跺€煎嵆涓簎ndefined銆?/p>

鎺ヤ笅鏉ワ紝浠嶇劧鍦ㄧ6琛岋紝鎴戜滑鐪嬪埌涓€涓祴鍊兼搷浣滅銆傛垜浠噯澶囩粰鍙橀噺b璧嬩竴涓柊鍊硷紝鎺ヤ笅鏉ユ垜浠湅鍒颁竴涓嚱鏁拌璋冪敤銆傚綋鐪嬪埌涓€涓彉閲忓悗闈㈣窡鐫€涓€涓渾鎷彿(鈥?鏃讹紝杩欏氨鏄皟鐢ㄥ嚱鏁扮殑淇″彿锛屾帴鐫€锛屾瘡涓嚱鏁伴兘杩斿洖涓€浜涗笢瑗?鍊笺€佸璞℃垨 undefined)锛屾棤璁轰粠鍑芥暟杩斿洖浠€涔堬紝閮藉皢璧嬪€肩粰鍙橀噺b銆?/p>

浣嗘槸棣栧厛鎴戜滑闇€瑕佽皟鐢╝ddTwo鐨勫嚱鏁般€侸avaScript灏嗗湪鍏跺叏灞€鎵ц涓婁笅鏂囧唴瀛樹腑鏌ユ壘鍚嶄负addTwo鐨勫彉閲忋€傚櫌锛屽畠鎵惧埌浜嗕竴涓紝瀹冩槸鍦ㄦ楠?(鎴栫2 - 5琛?涓畾涔夌殑銆傚彉閲廰dd2鍖呭惈涓€涓嚱鏁板畾涔夈€傛敞鎰忥紝鍙橀噺a浣滀负鍙傛暟浼犻€掔粰鍑芥暟銆侸avaScript鍦ㄥ叏灞€鎵ц涓婁笅鏂囧唴瀛樹腑鎼滅储鍙橀噺a锛屾壘鍒板畠锛屽彂鐜板畠鐨勫€兼槸3锛屽苟灏嗘暟瀛?浣滀负鍙傛暟浼犻€掔粰鍑芥暟锛屽噯澶囧ソ鎵ц鍑芥暟銆?/p>

鐜板湪鎵ц涓婁笅鏂囧皢鍒囨崲,鍒涘缓浜嗕竴涓柊鐨勬湰鍦版墽琛屼笂涓嬫枃锛屾垜浠皢鍏跺懡鍚嶄负鈥渁ddTwo鎵ц涓婁笅鏂団€濓紝鎵ц涓婁笅鏂囪鎺ㄩ€佸埌璋冪敤鍫嗘爤涓娿€傚湪addTwo鎵ц涓婁笅鏂囦腑锛屾垜浠鍋氱殑绗竴浠朵簨鏄粈涔?

浣犲彲鑳戒細璇达紝鈥滃湪addTwo鎵ц涓婁笅鏂囦腑澹版槑浜嗕竴涓柊鐨勫彉閲弐et鈥濓紝杩欐槸涓嶅鐨勩€傛纭殑绛旀鏄紝鎴戜滑闇€瑕佸厛鐪嬪嚱鏁扮殑鍙傛暟銆傚湪addTwo鎵ц涓婁笅鏂囦腑澹版槑涓€涓柊鐨勫彉閲廯`x```锛屽洜涓哄€?鏄綔涓哄弬鏁颁紶閫掔殑锛屾墍浠ュ彉閲弜琚祴鍊间负3銆?/p>

涓嬩竴姝ユ槸:鍦╝ddTwo鎵ц涓婁笅鏂囦腑澹版槑涓€涓柊鐨勫彉閲弐et銆傚畠鐨勫€艰璁剧疆涓?undefined(绗笁琛?銆?/p>

浠嶇劧鏄3琛岋紝闇€瑕佹墽琛屼竴涓浉鍔犳搷浣溿€傞鍏堟垜浠渶瑕亁鐨勫€硷紝JavaScript浼氬鎵句竴涓彉閲弜锛屽畠浼氶鍏堝湪addTwo鎵ц涓婁笅鏂囦腑瀵绘壘锛屾壘鍒颁簡涓€涓€间负3銆傜浜屼釜鎿嶄綔鏁版槸鏁板瓧2銆備袱涓浉鍔犵粨鏋滀负5灏辫鍒嗛厤缁欏彉閲弐et銆?/p>

绗?琛岋紝鎴戜滑杩斿洖鍙橀噺ret鐨勫唴瀹癸紝鍦╝ddTwo鎵ц涓婁笅鏂囦腑鏌ユ壘锛屾壘鍒板€间负5锛岃繑鍥烇紝鍑芥暟缁撴潫銆?/p>

绗?-5琛岋紝鍑芥暟缁撴潫銆俛ddTwo鎵ц涓婁笅鏂囪閿€姣侊紝鍙橀噺x鍜宺et琚噴鏀撅紝瀹冧滑宸茬粡涓嶅瓨鍦ㄤ簡銆俛ddTwo 鎵ц涓婁笅鏂囦粠璋冪敤鍫嗘爤涓脊鍑猴紝杩斿洖鍊艰繑鍥炵粰璋冪敤涓婁笅鏂囷紝鍦ㄨ繖绉嶆儏鍐典笅锛岃皟鐢ㄤ笂涓嬫枃鏄叏灞€鎵ц涓婁笅鏂囷紝鍥犱负鍑芥暟addTwo鏄粠鍏ㄥ眬鎵ц涓婁笅鏂囪皟鐢ㄧ殑銆?/p>

鐜板湪鎴戜滑缁х画绗?姝ョ殑鍐呭锛岃繑鍥炲€?琚垎閰嶇粰鍙橀噺b锛岀▼搴忎粛鐒跺湪绗?琛屻€?/p>

鍦ㄧ7琛岋紝b鐨勫€?5 琚墦鍗板埌鎺у埗鍙颁簡銆?/p>

瀵逛簬涓€涓潪甯哥畝鍗曠殑绋嬪簭锛岃繖鏄竴涓潪甯稿啑闀跨殑瑙i噴锛屾垜浠敋鑷宠繕娌℃湁娑夊強闂寘銆備絾鑲畾浼氭秹鍙婄殑锛屼笉杩囬鍏堟垜浠緱缁曚竴涓や釜寮€?/p>

璇嶆硶浣滅敤鍩燂紙Lexical scope锛?br/>鎴戜滑闇€瑕佺悊瑙h瘝娉曚綔鐢ㄥ煙鐨勪竴浜涚煡璇嗐€傝鐪嬩笅闈㈢殑渚嬪瓙锛?/p>

1: let val1 = 2
2: function multiplyThis(n) 
3:   let ret = n * val1
4:   return ret
5: 
6: let multiplied = multiplyThis(6)
7: console.log(鈥榚xample of scope:鈥? multiplied)

杩欓噷鎯宠鏄庯紝鎴戜滑鍦ㄥ嚱鏁版墽琛屼笂涓嬫枃涓湁鍙橀噺锛屽湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑鏈夊彉閲忋€侸avaScript鐨勪竴涓鏉備箣澶勫湪浜庡畠濡備綍鏌ユ壘鍙橀噺锛屽鏋滃湪鍑芥暟鎵ц涓婁笅鏂囦腑鎵句笉鍒板彉閲忥紝瀹冨皢鍦ㄨ皟鐢ㄤ笂涓嬫枃涓鎵惧畠锛屽鏋滃湪瀹冪殑璋冪敤涓婁笅鏂囦腑娌℃湁鎵惧埌锛屽氨涓€鐩村線涓婁竴绾э紝鐩村埌瀹冨湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑鏌ユ壘涓烘銆?濡傛灉鏈€鍚庢壘涓嶅埌锛屽畠灏辨槸 undefined)銆?br/>涓嬮潰鍒楀嚭鍚戜釜姝ラ鏉ヨВ閲婁竴涓嬶紙濡傛灉浣犲凡缁忕啛鎮変簡锛岃璺宠繃锛夛細

鍦ㄥ叏灞€鎵ц涓婁笅鏂囦腑澹版槑涓€涓柊鐨勫彉閲弙al1锛屽苟灏嗗叾璧嬪€间负2銆?/p>

绗?-5琛岋紝澹版槑涓€涓柊鐨勫彉閲?multiplyThis锛屽苟缁欏畠鍒嗛厤涓€涓嚱鏁板畾涔夈€?/p>

绗?琛岋紝澹版槑涓€涓湪鍏ㄥ眬鎵ц涓婁笅鏂?multiplied 鏂板彉閲忋€?/p>

浠庡叏灞€鎵ц涓婁笅鏂囧唴瀛樹腑鏌ユ壘鍙橀噺multiplyThis锛屽苟灏嗗叾浣滀负鍑芥暟鎵ц锛屼紶閫掓暟瀛?6 浣滀负鍙傛暟銆?/p>

鏂板嚱鏁拌皟鐢?鍒涘缓鏂版墽琛屼笂涓嬫枃锛夛紝鍒涘缓涓€涓柊鐨?multiplyThis 鍑芥暟鎵ц涓婁笅鏂囥€?/p>

鍦?multiplyThis 鎵ц涓婁笅鏂囦腑锛屽0鏄庝竴涓彉閲弉骞跺皢鍏惰祴鍊间负6銆?/p>

绗?3 琛屻€傚湪multiplyThis鎵ц涓婁笅鏂囦腑锛屽0鏄庝竴涓彉閲弐et銆?/p>

缁х画绗?3 琛屻€傚涓や釜鎿嶄綔鏁?n 鍜?val1 杩涜涔樻硶杩愮畻.鍦╩ultiplyThis鎵ц涓婁笅鏂囦腑鏌ユ壘鍙橀噺 n銆傛垜浠湪姝ラ6涓0鏄庝簡瀹?瀹冪殑鍐呭鏄暟瀛?銆傚湪multiplyThis鎵ц涓婁笅鏂囦腑鏌ユ壘鍙橀噺val1銆俶ultiplyThis鎵ц涓婁笅鏂囨病鏈変竴涓爣璁颁负 val1 鐨勫彉閲忋€傛垜浠悜璋冪敤涓婁笅鏂囨煡鎵撅紝璋冪敤涓婁笅鏂囨槸鍏ㄥ眬鎵ц涓婁笅鏂囷紝鍦ㄥ叏灞€鎵ц涓婁笅鏂囦腑瀵绘壘 val1銆傚摝锛屾槸鐨勩€佸湪閭e効锛屽畠鍦ㄦ楠?涓畾涔夛紝鏁板€兼槸2銆?/p>

缁х画绗?3 琛屻€傚皢涓や釜鎿嶄綔鏁扮浉涔樺苟灏嗗叾璧嬪€肩粰ret鍙橀噺锛? * 2 = 12锛宺et 鐜板湪鍊间负 12銆?/p>

杩斿洖ret鍙橀噺锛岄攢姣乵ultiplyThis鎵ц涓婁笅鏂囧強鍏跺彉閲?ret 鍜?n 銆傚彉閲?val1 娌℃湁琚攢姣侊紝鍥犱负瀹冩槸鍏ㄥ眬鎵ц涓婁笅鏂囩殑涓€閮ㄥ垎銆?/p>

鍥炲埌绗?琛屻€傚湪璋冪敤涓婁笅鏂囦腑锛屾暟瀛?12 璧嬪€肩粰 multiplied 鐨勫彉閲忋€?/p>

鏈€鍚庡湪绗?琛岋紝鎴戜滑鍦ㄦ帶鍒跺彴涓墦鍗?multiplied 鍙橀噺鐨勫€?/p>

鍦ㄨ繖涓緥瀛愪腑锛屾垜浠渶瑕佽浣忎竴涓嚱鏁板彲浠ヨ闂湪瀹冪殑璋冪敤涓婁笅鏂囦腑瀹氫箟鐨勫彉閲忥紝杩欎釜灏辨槸璇嶆硶浣滅敤鍩燂紙Lexical scope锛夈€?/p>

杩斿洖鍑芥暟鐨勫嚱鏁?/h2>

鍦ㄧ涓€涓緥瀛愪腑锛屽嚱鏁癮ddTwo杩斿洖涓€涓暟瀛椼€傝璁颁綇锛屽嚱鏁板彲浠ヨ繑鍥炰换浣曚笢瑗裤€傝鎴戜滑鐪嬩竴涓繑鍥炲嚱鏁扮殑鍑芥暟绀轰緥锛屽洜涓鸿繖瀵逛簬鐞嗚В闂寘闈炲父閲嶈銆傜湅绮熷瓙锛?/p>

 1: let val = 7
 2: function createAdder() 
 3:   function addNumbers(a, b) 
 4:     let ret = a + b
 5:     return ret
 6:   
 7:   return addNumbers
 8: 
 9: let adder = createAdder()
10: let sum = adder(val, 8)
11: console.log(鈥榚xample of function returning a function: 鈥? sum)

璁╂垜浠洖鍒板垎姝ュ垎瑙o細

绗?琛屻€傛垜浠湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑澹版槑涓€涓彉閲弙al骞惰祴鍊间负 7銆?/p>

绗?2-8 琛屻€傛垜浠湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑澹版槑浜嗕竴涓悕涓?createAdder 鐨勫彉閲忥紝骞朵负鍏跺垎閰嶄簡涓€涓嚱鏁板畾涔夈€傜3-7琛屾弿杩颁簡涓婅堪鍑芥暟瀹氫箟锛屽拰浠ュ墠涓€鏍凤紝鍦ㄨ繖涓€鐐逛笂锛屾垜浠病鏈夌洿鎺ヨ璁鸿繖涓嚱鏁般€傛垜浠彧鏄皢鍑芥暟瀹氫箟瀛樺偍鍒伴偅涓彉閲?createAdder)涓€?/p>

绗?琛屻€傛垜浠湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑澹版槑浜嗕竴涓悕涓?adder 鐨勬柊鍙橀噺锛屾殏鏃讹紝鍊间负 undefined銆?/p>

绗?琛屻€傛垜浠湅鍒版嫭鍙?)锛屾垜浠渶瑕佹墽琛屾垨璋冪敤涓€涓嚱鏁帮紝鏌ユ壘鍏ㄥ眬鎵ц涓婁笅鏂囩殑鍐呭瓨骞舵煡鎵惧悕涓篶reateAdder 鐨勫彉閲忥紝瀹冩槸鍦ㄦ楠?涓垱寤虹殑銆傚ソ鍚э紝鎴戜滑璋冪敤瀹冦€?/p>

璋冪敤鍑芥暟鏃讹紝鎵ц鍒扮2琛屻€傚垱寤轰竴涓柊鐨刢reateAdder鎵ц涓婁笅鏂囥€傛垜浠彲浠ュ湪createAdder鐨勬墽琛屼笂涓嬫枃涓垱寤鸿嚜鏈夊彉閲忋€俲s 寮曟搸灏哻reateAdder鐨勪笂涓嬫枃娣诲姞鍒拌皟鐢ㄥ爢鏍堛€傝繖涓嚱鏁版病鏈夊弬鏁帮紝璁╂垜浠洿鎺ヨ烦鍒板畠鐨勪富浣撻儴鍒?

绗?3-6 琛屻€傛垜浠湁涓€涓柊鐨勫嚱鏁板0鏄庯紝鎴戜滑鍦╟reateAdder鎵ц涓婁笅鏂囦腑鍒涘缓涓€涓彉閲廰ddNumbers銆傝繖寰堥噸瑕侊紝addnumber鍙瓨鍦ㄤ簬createAdder鎵ц涓婁笅鏂囦腑銆傛垜浠皢鍑芥暟瀹氫箟瀛樺偍鍦ㄥ悕涓?``addNumbers``` 鐨勮嚜鏈夊彉閲忎腑銆?/p>

绗?琛岋紝鎴戜滑杩斿洖鍙橀噺addNumbers鐨勫唴瀹广€俲s寮曟搸鏌ユ壘涓€涓悕涓篴ddNumbers鐨勫彉閲忓苟鎵惧埌瀹冿紝杩欐槸涓€涓嚱鏁板畾涔夈€傚ソ鐨勶紝鍑芥暟鍙互杩斿洖浠讳綍涓滆タ锛屽寘鎷嚱鏁板畾涔夈€傛垜浠繑addNumbers鐨勫畾涔夈€傜4琛屽拰绗?琛屾嫭鍙蜂箣闂寸殑鍐呭鏋勬垚璇ュ嚱鏁板畾涔夈€?/p>

杩斿洖鏃讹紝createAdder鎵ц涓婁笅鏂囧皢琚攢姣併€俛ddNumbers 鍙橀噺涓嶅啀瀛樺湪銆備絾addNumbers鍑芥暟瀹氫箟浠嶇劧瀛樺湪锛屽洜涓哄畠杩斿洖骞惰祴鍊肩粰浜哸dder 鍙橀噺銆?/p>

绗?0琛屻€傛垜浠湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑瀹氫箟浜嗕竴涓柊鐨勫彉閲?sum锛屽厛璧嬪€间负 undefined;

鎺ヤ笅鏉ユ垜浠渶瑕佹墽琛屼竴涓嚱鏁般€傚摢涓嚱鏁? 鏄悕涓篴dder鍙橀噺涓畾涔夌殑鍑芥暟銆傛垜浠湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑鏌ユ壘瀹冿紝鏋滅劧鎵惧埌浜嗗畠锛岃繖涓嚱鏁版湁涓や釜鍙傛暟銆?/p>

璁╂垜浠煡鎵捐繖涓や釜鍙傛暟锛岀涓€涓槸鎴戜滑鍦ㄦ楠?涓畾涔夌殑鍙橀噺val锛屽畠琛ㄧず鏁板瓧7锛岀浜屼釜鏄暟瀛?銆?/p>

鐜板湪鎴戜滑瑕佹墽琛岃繖涓嚱鏁帮紝鍑芥暟瀹氫箟姒傝堪鍦ㄧ3-5琛岋紝鍥犱负杩欎釜鍑芥暟鏄尶鍚嶏紝涓轰簡鏂逛究鐞嗚В锛屾垜浠殏涓斿彨瀹僡dder鍚с€傝繖鏃跺垱寤轰竴涓猘dder鍑芥暟鎵ц涓婁笅鏂囷紝鍦╝dder鎵ц涓婁笅鏂囦腑鍒涘缓浜嗕袱涓柊鍙橀噺 a 鍜?b銆傚畠浠垎鍒璧嬪€间负 7 鍜?8锛屽洜涓鸿繖浜涙槸鎴戜滑鍦ㄤ笂涓€姝ヤ紶閫掔粰鍑芥暟鐨勫弬鏁般€?/p>

绗?4 琛屻€傚湪adder鎵ц涓婁笅鏂囦腑澹版槑浜嗕竴涓悕涓簉et鐨勬柊鍙橀噺,

绗?4 琛屻€傚皢鍙橀噺a鐨勫唴瀹瑰拰鍙橀噺b鐨勫唴瀹圭浉鍔犲緱15骞惰祴缁檙et鍙橀噺銆?/p>

ret鍙橀噺浠庤鍑芥暟杩斿洖銆傝繖涓尶鍚嶅嚱鏁版墽琛屼笂涓嬫枃琚攢姣侊紝浠庤皟鐢ㄥ爢鏍堜腑鍒犻櫎锛屽彉閲廰銆乥鍜宺et涓嶅啀瀛樺湪銆?/p>

杩斿洖鍊艰鍒嗛厤缁欐垜浠湪姝ラ9涓畾涔夌殑sum鍙橀噺銆?/p>

鎴戜滑灏唖um鐨勫€兼墦鍗板埌鎺у埗鍙般€?/p>

濡傞鏈燂紝鎺у埗鍙板皢鎵撳嵃15銆傛垜浠湪杩欓噷纭疄缁忓巻浜嗗緢澶氬洶闅撅紝鎴戞兂鍦ㄨ繖閲岃鏄庡嚑鐐广€傞鍏堬紝鍑芥暟瀹氫箟鍙互瀛樺偍鍦ㄥ彉閲忎腑锛屽嚱鏁板畾涔夊湪绋嬪簭璋冪敤涔嬪墠鏄笉鍙鐨勩€傚叾娆★紝姣忔璋冪敤鍑芥暟鏃讹紝閮戒細(涓存椂)鍒涘缓涓€涓湰鍦版墽琛屼笂涓嬫枃銆傚綋鍑芥暟瀹屾垚鏃讹紝鎵ц涓婁笅鏂囧皢娑堝け銆傚嚱鏁板湪閬囧埌return鎴栧彸鎷彿鏃舵墽琛屽畬鎴愩€?/p>

鏈€鍚?涓€涓棴鍖?/h2>

鐪嬬湅涓嬮潰鐨勪唬鐮侊紝骞惰瘯鐫€寮勬竻妤氫細鍙戠敓浠€涔堛€?/p>

1: function createCounter() 
 2:   let counter = 0
 3:   const myFunction = function() 
 4:     counter = counter + 1
 5:     return counter
 6:   
 7:   return myFunction
 8: 
 9: const increment = createCounter()
10: const c1 = increment()
11: const c2 = increment()
12: const c3 = increment()
13: console.log(鈥榚xample increment鈥? c1, c2, c3)

鐜板湪锛屾垜浠凡缁忎粠鍓嶄袱涓ず渚嬩腑鎺屾彙浜嗗畠鐨勮瘈绐嶏紝璁╂垜浠寜鐓ч鏈熺殑鏂瑰紡蹇€熸墽琛屽畠锛?/p>

绗?1-8 琛屻€傛垜浠湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑鍒涘缓浜嗕竴涓柊鐨勫彉閲廲reateCounter锛屽苟璧嬪€间簡涓€涓殑鍑芥暟瀹氫箟銆?/p>

绗?琛屻€傛垜浠湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑澹版槑浜嗕竴涓悕涓篿ncrement鐨勬柊鍙橀噺銆?/p>

绗?琛屻€傛垜浠渶瑕佽皟鐢╟reateCounter鍑芥暟骞跺皢鍏惰繑鍥炲€艰祴缁檌ncrement鍙橀噺銆?/p>

绗?1-8琛屻€傝皟鐢ㄥ嚱鏁帮紝鍒涘缓鏂扮殑鏈湴鎵ц涓婁笅鏂囥€?/p>

绗?琛屻€傚湪鏈湴鎵ц涓婁笅鏂囦腑锛屽0鏄庝竴涓悕涓篶ounter鐨勬柊鍙橀噺骞惰祴鍊间负 0;

绗?3-6琛屻€傚0鏄庝竴涓悕涓簃yFunction鐨勬柊鍙橀噺锛屽彉閲忓湪鏈湴鎵ц涓婁笅鏂囦腑澹版槑,鍙橀噺鐨勫唴瀹规槸涓虹4琛屽拰绗?琛屾墍瀹氫箟銆?/p>

绗?琛屻€傝繑鍥瀖yFunction鍙橀噺鐨勫唴瀹癸紝鍒犻櫎鏈湴鎵ц涓婁笅鏂囥€傚彉閲弇yFunction鍜宑ounter涓嶅啀瀛樺湪銆傛鏃舵帶鍒舵潈鍥炲埌浜嗚皟鐢ㄤ笂涓嬫枃銆?/p>

绗?琛屻€傚湪璋冪敤涓婁笅鏂?鍏ㄥ眬鎵ц涓婁笅鏂?涓紝createCounter杩斿洖鐨勫€艰祴缁欎簡increment锛屽彉閲廼ncrement鐜板湪鍖呭惈涓€涓嚱鏁板畾涔夊唴瀹逛负createCounter杩斿洖鐨勫嚱鏁般€傚畠涓嶅啀鏍囪涓簃yFunction`锛屼絾瀹冪殑瀹氫箟鏄浉鍚岀殑銆傚湪鍏ㄥ眬涓婁笅鏂囦腑锛屽畠鏄殑鏍囪涓簂abeledincrement銆?/p>

绗?0琛屻€傚0鏄庝竴涓柊鍙橀噺 c1銆?/p>

缁х画绗?0琛屻€傛煡鎵緄ncrement鍙橀噺锛屽畠鏄竴涓嚱鏁板苟璋冪敤瀹冦€傚畠鍖呭惈鍓嶉潰杩斿洖鐨勫嚱鏁板畾涔夛紝濡傜4-5琛屾墍瀹氫箟鐨勩€?/p>

鍒涘缓涓€涓柊鐨勬墽琛屼笂涓嬫枃銆傛病鏈夊弬鏁帮紝寮€濮嬫墽琛屽嚱鏁般€?/p>

绗?琛屻€俢ounter=counter + 1銆傚湪鏈湴鎵ц涓婁笅鏂囦腑鏌ユ壘counter鍙橀噺銆傛垜浠彧鏄垱寤轰簡閭d釜涓婁笅鏂囷紝浠庢潵娌℃湁澹版槑浠讳綍灞€閮ㄥ彉閲忋€傝鎴戜滑鐪嬬湅鍏ㄥ眬鎵ц涓婁笅鏂囥€傝繖閲屼篃娌℃湁counter鍙橀噺銆侸avascript浼氬皢鍏惰绠椾负counter = undefined + 1锛屽0鏄庝竴涓爣璁颁负counter鐨勬柊灞€閮ㄥ彉閲忥紝骞跺皢鍏惰祴鍊间负number 1锛屽洜涓簎ndefined琚綋浣滃€间负 0銆?/p>

绗?琛屻€傛垜浠彉閲廲ounter鐨勫€?1锛屾垜浠攢姣佹湰鍦版墽琛屼笂涓嬫枃鍜宑ounter鍙橀噺銆?/p>

鍥炲埌绗?0琛屻€傝繑鍥炲€?琚祴缁檆1銆?/p>

绗?1琛屻€傞噸澶嶆楠?0-14锛宑2涔熻璧嬪€间负1銆?/p>

绗?2琛屻€傞噸澶嶆楠?0-14锛宑3涔熻璧嬪€间负1銆?/p>

绗?3琛屻€傛垜浠墦鍗板彉閲廲1 c2鍜宑3鐨勫唴瀹广€?/p>

浣犺嚜宸辫瘯璇曪紝鐪嬬湅浼氬彂鐢熶粈涔堛€備綘浼氬皢娉ㄦ剰鍒帮紝瀹冨苟涓嶅儚浠庢垜涓婇潰鐨勮В閲婁腑鎵€鏈熸湜鐨勯偅鏍疯褰?,1,1銆傝€屾槸璁板綍1,2,3銆傝繖涓槸涓轰粈涔?
涓嶇煡鎬庝箞婊达紝increment鍑芥暟璁颁綇浜嗛偅涓猚unter鐨勫€笺€傝繖鏄€庝箞鍥炰簨?
counter鏄叏灞€鎵ц涓婁笅鏂囩殑涓€閮ㄥ垎鍚?灏濊瘯 console.log(counter)锛屽緱鍒皍ndefined鐨勭粨鏋?鏄剧劧涓嶆槸杩欐牱鐨勩€?br/>涔熻锛屽綋浣犺皟鐢╥ncrement鏃讹紝瀹冧細浠ユ煇绉嶆柟寮忚繑鍥炲畠鍒涘缓鐨勫嚱鏁?createCounter)?杩欐€庝箞鍙兘鍛?鍙橀噺increment鍖呭惈鍑芥暟瀹氫箟锛岃€屼笉鏄嚱鏁扮殑鏉ユ簮锛屾樉鐒朵篃涓嶆槸杩欐牱鐨勩€?br/>鎵€浠ヤ竴瀹氭湁鍙︿竴绉嶆満鍒躲€傞棴鍖咃紝鎴戜滑缁堜簬鎵惧埌浜嗭紝涓㈠け鐨勯偅鍧椼€?br/>瀹冩槸杩欐牱宸ヤ綔鐨勶紝鏃犺浣曟椂澹版槑鏂板嚱鏁板苟灏嗗叾璧嬪€肩粰鍙橀噺锛岄兘瑕佸瓨鍌ㄥ嚱鏁板畾涔夊拰闂寘銆傞棴鍖呭寘鍚湪鍑芥暟鍒涘缓鏃朵綔鐢ㄥ煙涓殑鎵€鏈夊彉閲忥紝瀹冪被浼间簬鑳屽寘銆傚嚱鏁板畾涔夐檮甯︿竴涓皬鑳屽寘锛屽畠鐨勫寘涓瓨鍌ㄤ簡鍑芥暟瀹氫箟鍒涘缓鏃朵綔鐢ㄥ煙涓殑鎵€鏈夊彉閲忋€?br/>鎵€浠ユ垜浠笂闈㈢殑瑙i噴閮芥槸閿欑殑锛岃鎴戜滑鍐嶈瘯涓€娆★紝浣嗘槸杩欐鏄纭殑銆?/p>

1: function createCounter() 
 2:   let counter = 0
 3:   const myFunction = function() 
 4:     counter = counter + 1
 5:     return counter
 6:   
 7:   return myFunction
 8: 
 9: const increment = createCounter()
10: const c1 = increment()
11: const c2 = increment()
12: const c3 = increment()
13: console.log(鈥榚xample increment鈥? c1, c2, c3)

鍚屼笂锛岀1-8琛屻€傛垜浠湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑鍒涘缓浜嗕竴涓柊鐨勫彉閲廲reateCounter锛屽畠寰楀埌浜嗘寚瀹氱殑鍑芥暟瀹氫箟銆?/p>

鍚屼笂锛岀9琛屻€傛垜浠湪鍏ㄥ眬鎵ц涓婁笅鏂囦腑澹版槑浜嗕竴涓悕涓篿ncrement鐨勬柊鍙橀噺銆?/p>

鍚屼笂锛岀9琛屻€傛垜浠渶瑕佽皟鐢╟reateCounter鍑芥暟骞跺皢鍏惰繑鍥炲€艰祴缁檌ncrement鍙橀噺銆?/p>

鍚屼笂锛岀1-8琛屻€傝皟鐢ㄥ嚱鏁帮紝鍒涘缓鏂扮殑鏈湴鎵ц涓婁笅鏂囥€?/p>

鍚屼笂锛岀2琛屻€傚湪鏈湴鎵ц涓婁笅鏂囦腑锛屽0鏄庝竴涓悕涓篶ounter鐨勬柊鍙橀噺骞惰祴鍊间负 0 銆?/p>

绗?-6琛屻€傚0鏄庝竴涓悕涓簃yFunction鐨勬柊鍙橀噺锛屽彉閲忓湪鏈湴鎵ц涓婁笅鏂囦腑澹版槑,鍙橀噺鐨勫唴瀹规槸鍙︿竴涓嚱鏁板畾涔夈€傚绗?琛屽拰绗?琛屾墍瀹氫箟锛岀幇鍦ㄦ垜浠繕鍒涘缓浜嗕竴涓棴鍖咃紝骞跺皢鍏朵綔涓哄嚱鏁板畾涔夌殑涓€閮ㄥ垎銆傞棴鍖呭寘鍚綔鐢ㄥ煙涓殑鍙橀噺锛屽湪鏈緥涓槸鍙橀噺counter(鍊间负0)銆?/p>

绗?琛屻€傝繑鍥瀖yFunction鍙橀噺鐨勫唴瀹?鍒犻櫎鏈湴鎵ц涓婁笅鏂囥€俶yFunction鍜宑ounter涓嶅啀瀛樺湪銆傛帶鍒舵潈浜ょ粰浜嗚皟鐢ㄤ笂涓嬫枃锛屾垜浠繑鍥炲嚱鏁板畾涔夊拰瀹冪殑闂寘锛岄棴鍖呬腑鍖呭惈浜嗗垱寤哄畠鏃跺湪浣滅敤鍩熷唴鐨勫彉閲忋€?/p>

绗?琛屻€傚湪璋冪敤涓婁笅鏂?鍏ㄥ眬鎵ц涓婁笅鏂?涓紝createCounter杩斿洖鐨勫€艰鎸囧畾涓篿ncrement锛屽彉閲廼ncrement鐜板湪鍖呭惈涓€涓嚱鏁板畾涔?鍜岄棴鍖?,鐢眂reateCounter杩斿洖鐨勫嚱鏁板畾涔?瀹冧笉鍐嶆爣璁颁负myFunction锛屼絾瀹冪殑瀹氫箟鏄浉鍚岀殑,鍦ㄥ叏灞€涓婁笅鏂囦腑锛岀О涓篿ncrement銆?/p>

绗?0琛屻€傚0鏄庝竴涓柊鍙橀噺c1銆?/p>

缁х画绗?0琛屻€傛煡鎵惧彉閲廼ncrement锛屽畠鏄竴涓嚱鏁帮紝璋冪敤瀹冦€傚畠鍖呭惈鍓嶉潰杩斿洖鐨勫嚱鏁板畾涔?濡傜4-5琛屾墍瀹氫箟鐨勩€?瀹冭繕鏈変竴涓甫鏈夊彉閲忕殑闂寘)銆?/p>

鍒涘缓涓€涓柊鐨勬墽琛屼笂涓嬫枃锛屾病鏈夊弬鏁帮紝寮€濮嬫墽琛屽嚱鏁般€?/p>

绗?琛屻€俢ounter = counter + 1锛屽鎵惧彉閲?counter锛屽湪鏌ユ壘鏈湴鎴栧叏灞€鎵ц涓婁笅鏂囦箣鍓嶏紝璁╂垜浠鏌ヤ竴涓嬮棴鍖咃紝鐬э紝闂寘鍖呭惈涓€涓悕涓篶ounter鐨勫彉閲忥紝鍏跺€间负0銆傚湪绗?琛岃〃杈惧紡涔嬪悗锛屽畠鐨勫€艰璁剧疆涓?銆傚畠鍐嶆琚偍瀛樺湪闂寘閲岋紝闂寘鐜板湪鍖呭惈鍊间负1鐨勫彉閲?counter銆?/p>

绗?琛屻€傛垜浠繑鍥瀋ounter鐨勫€硷紝閿€姣佹湰鍦版墽琛屼笂涓嬫枃銆?/p>

鍥炲埌绗?0琛屻€傝繑鍥炲€?琚祴缁欏彉閲廲1銆?/p>

绗?1琛屻€傛垜浠噸澶嶆楠?0-14銆傝繖涓€娆★紝鍦ㄩ棴鍖呬腑姝ゆ椂鍙橀噺counter鐨勫€兼槸1銆傚畠鍦ㄧ12琛岃缃殑锛屽畠鐨勫€艰閫掑骞朵互2鐨勫舰寮忓瓨鍌ㄥ湪閫掑鍑芥暟鐨勯棴鍖呬腑,c2琚祴鍊间负2銆?/p>

绗?2琛屻€傞噸澶嶆楠?0-14琛?c3琚祴鍊间负3銆?/p>

绗?3琛屻€傛垜浠墦鍗板彉閲廲1 c2鍜宑3鐨勫€笺€?/p>

浣犲彲鑳戒細闂紝鏄惁鏈変换浣曞嚱鏁板叿鏈夐棴鍖咃紝鐢氳嚦鏄湪鍏ㄥ眬鑼冨洿鍐呭垱寤虹殑鍑芥暟?绛旀鏄偗瀹氱殑銆傚湪鍏ㄥ眬浣滅敤鍩熶腑鍒涘缓鐨勫嚱鏁板垱寤洪棴鍖咃紝浣嗘槸鐢变簬杩欎簺鍑芥暟鏄湪鍏ㄥ眬浣滅敤鍩熶腑鍒涘缓鐨勶紝鎵€浠ュ畠浠彲浠ヨ闂叏灞€浣滅敤鍩熶腑鐨勬墍鏈夊彉閲忥紝闂寘鐨勬蹇靛苟涓嶉噸瑕併€?br/>褰撳嚱鏁拌繑鍥炲嚱鏁版椂锛岄棴鍖呯殑姒傚康灏卞彉寰楁洿鍔犻噸瑕佷簡銆傝繑鍥炵殑鍑芥暟鍙互璁块棶涓嶅睘浜庡叏灞€浣滅敤鍩熺殑鍙橀噺锛屼絾瀹冧滑浠呭瓨鍦ㄤ簬鍏堕棴鍖呬腑銆?/p>

闂寘涓嶆槸閭d箞绠€鍗?/h2>

鏈夋椂鍊欓棴鍖呭湪浣犵敋鑷虫病鏈夋敞鎰忓埌瀹冪殑鏃跺€欏氨浼氬嚭鐜帮紝浣犲彲鑳藉凡缁忕湅鍒颁簡鎴戜滑绉颁负閮ㄥ垎搴旂敤绋嬪簭鐨勭ず渚嬶紝濡備笅闈㈢殑浠g爜鎵€绀猴細

let c = 4
const addX = x => n => n + x
const addThree = addX(3)
let d = addThree(c)
console.log(鈥榚xample partial application鈥? d)

濡傛灉绠ご鍑芥暟璁╀綘鎰熷埌鍥版儜锛屼笅闈㈡槸鍚屾牱鏁堟灉锛?/p>

let c = 4
function addX(x) 
  return function(n) 
     return n + x
  

const addThree = addX(3)
let d = addThree(c)
console.log(鈥榚xample partial application鈥? d)

鎴戜滑澹版槑涓€涓兘鐢ㄥ姞娉曞嚱鏁癮ddX锛屽畠鎺ュ彈涓€涓弬鏁皒骞惰繑鍥炲彟涓€涓嚱鏁般€傝繑鍥炵殑鍑芥暟杩樻帴鍙椾竴涓弬鏁板苟灏嗗叾娣诲姞鍒板彉閲弜涓€?br/>鍙橀噺x鏄棴鍖呯殑涓€閮ㄥ垎锛屽綋鍙橀噺addThree鍦ㄦ湰鍦颁笂涓嬫枃涓0鏄庢椂锛屽畠琚垎閰嶄竴涓嚱鏁板畾涔夊拰涓€涓棴鍖咃紝闂寘鍖呭惈鍙橀噺x銆?br/>鎵€浠ュ綋addThree琚皟鐢ㄥ苟鎵ц鏃讹紝瀹冨彲浠ヤ粠闂寘涓闂彉閲弜浠ュ強涓哄弬鏁颁紶閫掑彉閲弉骞惰繑鍥炰袱鑰呯殑鍜?7銆?/p>

鎬荤粨

鎴戝皢姘歌繙璁颁綇闂寘鐨勬柟娉曟槸閫氳繃鑳屽寘鐨勭被姣斻€傚綋涓€涓嚱鏁拌鍒涘缓骞朵紶閫掓垨浠庡彟涓€涓嚱鏁拌繑鍥炴椂锛屽畠浼氭惡甯︿竴涓儗鍖呫€傝儗鍖呬腑鏄嚱鏁板0鏄庢椂浣滅敤鍩熷唴鐨勬墍鏈夊彉閲忋€?/p>

以上是关于鎴戜粠鏉ヤ笉鐞嗚ВJavaScript闂寘锛岀洿鍒版湁浜鸿繖鏍峰悜鎴戣В閲婂畠的主要内容,如果未能解决你的问题,请参考以下文章

鎬庝箞鐞嗚В鈥滈潪瀵圭О鍔犲瘑鈥濓紵

鎴戜笌鎬濆惁锛屼竷骞翠箣鐥?| 鎬濆惁 9 鍛ㄥ勾

鍒嗗竷寮忎簨鍔$殑CAP鐞嗚

鎴戜滑绗竴娆″彂鐨凪arvin绯诲垪鑵曡〃

缁堜簬鏉ヤ簡锛佺粨鍚?Laravel 5.3 + vue 2.1 缁勪欢鍖栧紑鍙戠殑鍗氬绯荤粺