鎴戜粠鏉ヤ笉鐞嗚В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>
- JavaScript鍒涘缓涓€涓柊鐨勬墽琛屼笂涓嬫枃锛屾垜浠彨浣滄湰鍦版墽琛屼笂涓嬫枃銆?/li>
- 杩欎釜鏈湴鎵ц涓婁笅鏂囧皢鏈夊畠鑷繁鐨勪竴缁勫彉閲忥紝杩欎簺鍙橀噺灏嗘槸杩欎釜鎵ц涓婁笅鏂囩殑鏈?鍦板彉閲忋€?/li>
- 鏂扮殑鎵ц涓婁笅鏂囪鎺ㄥ埌鍒版墽琛屽爢鏍堜腑銆傚彲浠ュ皢鎵ц鍫嗘爤鐪嬩綔鏄竴绉嶄繚瀛樼▼搴忓湪鍏舵墽琛屼腑鐨勪綅缃殑瀹瑰櫒銆?/li>
鍑芥暟浠€涔堟椂鍊欑粨鏉?褰撳畠閬囧埌涓€涓猺eturn璇彞鎴栦竴涓粨鏉熸嫭鍙穧銆?/p>
褰撲竴涓嚱鏁扮粨鏉熸椂锛屼細鍙戠敓浠ヤ笅鎯呭喌:
- 杩欎釜鏈湴鎵ц涓婁笅鏂囦粠鎵ц鍫嗘爤涓脊鍑恒€?/li>
-
鍑芥暟灏嗚繑鍥炲€艰繑鍥炶皟鐢ㄤ笂涓嬫枃銆傝皟鐢ㄤ笂涓嬫枃鏄皟鐢ㄨ繖涓湰鍦扮殑鎵ц涓婁笅鏂囷紝瀹冨彲浠ユ槸鍏ㄥ眬鎵ц涓婁笅鏂囷紝涔熷彲浠ユ槸鍙﹀涓€涓湰鍦扮殑鎵ц涓婁笅鏂囥€傝繖鍙栧喅浜庤皟鐢ㄦ墽琛屼笂涓嬫枃鏉ュ鐞嗘鏃剁殑杩斿洖鍊硷紝杩斿洖鐨勫€煎彲浠ユ槸涓€涓璞°€佷竴涓暟缁勩€佷竴涓嚱鏁般€佷竴涓竷灏斿€肩瓑绛夛紝濡傛灉鍑芥暟娌℃湁return璇彞锛屽垯杩斿洖undefined銆?/p>
- 杩欎釜鏈湴鎵ц涓婁笅鏂囪閿€姣侊紝閿€姣佹槸寰堥噸瑕侊紝杩欎釜鏈湴鎵ц涓婁笅鏂囦腑澹版槑鐨勬墍鏈夊彉閲忛兘灏嗚鍒犻櫎锛屼笉鍦ㄦ湁鍙橀噺锛岃繖涓氨鏄负浠€涔?绉颁负鏈湴鎵ц涓婁笅鏂囦腑鑷湁鐨勫彉閲忋€?/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闂寘锛岀洿鍒版湁浜鸿繖鏍峰悜鎴戣В閲婂畠的主要内容,如果未能解决你的问题,请参考以下文章