jsÖÐʲôÊDZհüº¯Êý
±Õ°üº¯ÊýÊÇÓµÓÐ×ÔÓɱäÁ¿£¬×ÝÈ»ÔÚ½¨ÉèËüµÄÉÏÏÂÎÄ¿¢Êºó£¬ÈÔÈ»¿ÉÒÔ»á¼ûÍⲿÉùÃ÷±äÁ¿µÄº¯Êý¡£ÆäÔÀíÊÇ£ºÄÚ²¿º¯Êý¿ÉÒÔ»á¼ûÍⲿº¯ÊýµÄ×÷ÓÃÓò±äÁ¿¡£±Õ°üº¯Êý½¨Éè×Ô¼ºµÄ´Ê·¨ÇéÐΣ¬ÆäÖаüÀ¨Íⲿº¯ÊýµÄ×÷ÓÃÓò±äÁ¿¡£±Õ°üº¯Êý¿ÉÒÔ»á¼ûÆä´Ê·¨ÇéÐÎÖÐÉúÑĵÄ×ÔÓɱäÁ¿£¬×ÝÈ»Íⲿº¯ÊýÒÑÖ´ÐÐÍê±Ï¡£
ʲôÊDZհüº¯Êý£¿
ÔÚ JavaScript ÖУ¬±Õ°üº¯ÊýÊÇÒ»¸öÓµÓÐ×ÔÓɱäÁ¿µÄº¯Êý£¬×ÔÓɱäÁ¿ÊÇÖ¸ÔÚº¯ÊýÍⲿÉùÃ÷µÄ±äÁ¿¡£×ÝÈ»ÔÚ½¨ÉèËüµÄÉÏÏÂÎÄ¿¢Êºó£¬ËüÈÔÈ»¿ÉÒÔ»á¼ûÕâЩ±äÁ¿¡£
±Õ°üº¯ÊýµÄÔÀí
±Õ°üº¯ÊýµÄÊÂÇéÔÀíÈçÏ£º
JavaScript µÄ×÷ÓÃÓò¹æÔòÔÊÐíÄÚ²¿º¯Êý»á¼ûÍⲿº¯ÊýµÄ×÷ÓÃÓò¡£
±Õ°üº¯ÊýÔÚ±»Ö´ÐÐʱ£¬»á½¨ÉèÆä×Ô¼ºµÄ´Ê·¨ÇéÐΣ¬ÆäÖаüÀ¨Íⲿº¯ÊýµÄ×÷ÓÃÓò±äÁ¿¡£
×ÝÈ»Íⲿº¯ÊýÒѾִÐÐÍê±Ï£¬±Õ°üº¯ÊýÈÔÈ»¿ÉÒÔ»á¼ûÆä´Ê·¨ÇéÐÎÖеıäÁ¿£¬ÕâЩ±äÁ¿±»³ÆΪ×ÔÓɱäÁ¿¡£
±Õ°üº¯ÊýµÄÓÅÊÆ
±Õ°üº¯ÊýÓÐÒÔÏÂÓÅÊÆ£º
Êý¾Ý˽Óл¯£º×ÔÓɱäÁ¿¿ÉÒÔ·â×°ÔÚ±Õ°üº¯ÊýÖУ¬Ê¹Æä¶ÔÍⲿ´úÂë²»¿É¼û£¬´Ó¶øʵÏÖÊý¾Ý˽Óл¯¡£
״̬ÖÎÀí£º±Õ°üº¯Êý¿ÉÒÔ´æ´¢ºÍÖÎÀí״̬£¬×ÝÈ»ÔÚÍⲿº¯ÊýÖ´ÐÐÍê±Ïºó£¬×´Ì¬ÈÔÈ»¿ÉÒÔ±£´æ¡£
»Øµ÷º¯Êý£º±Õ°üº¯Êý¿ÉÒÔ×÷Ϊ»Øµ÷º¯Êý£¬ÔÚÒì²½²Ù×÷Íê³ÉºóÖ´ÐÐÌض¨Ê¹Ãü¡£
±Õ°üº¯ÊýµÄʾÀý
ÒÔÏÂÊÇÒ»¸ö±Õ°üº¯ÊýµÄʾÀý£º
function outerFunction(x) { let y = 10; function innerFunction() { return x + y; } return innerFunction; } const inner = outerFunction(5); console.log(inner()); // Êä³ö£º15
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬innerFunction ÊÇÒ»¸ö±Õ°üº¯Êý£¬ËüÓµÓÐ×ÔÓɱäÁ¿ x ºÍ y¡£×ÝÈ» outerFunction ÒѾִÐÐÍê±Ï£¬innerFunction ÈÔÈ»¿ÉÒÔ»á¼û x ºÍ y¡£
ÒÔÉϾÍÊÇjsÖÐʲôÊDZհüº¯ÊýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡