{"id":166392,"date":"2024-05-08T15:35:39","date_gmt":"2024-05-08T07:35:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/166392.html"},"modified":"2024-05-08T15:35:43","modified_gmt":"2024-05-08T07:35:43","slug":"javascript%e5%9c%a8%e4%b8%80%e4%b8%aa%e5%85%a8%e5%b1%80%e5%87%bd%e6%95%b0%e4%b8%ad%e8%b0%83%e7%94%a8%e5%8f%a6%e4%b8%80%e4%b8%aa%e5%85%a8%e5%b1%80%e5%87%bd%e6%95%b0%ef%bc%8c%e5%86%85%e5%ad%98%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/166392.html","title":{"rendered":"Javascript\u5728\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\uff0c\u5185\u5b58\u4e2d\u662f\u600e\u6837\u7684"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26233453\/8cb8bc58-bad0-4e91-8eb8-bc8b0e82f6f4.webp\" alt=\"Javascript\u5728\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\uff0c\u5185\u5b58\u4e2d\u662f\u600e\u6837\u7684\" \/><\/p>\n<p><p>JavaScript\u5728\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u65f6\uff0c<strong>\u5185\u5b58\u5806\u6808\u4e2d\u4f1a\u53d1\u751f\u51fd\u6570\u8c03\u7528\u3001\u6267\u884c\u4e0a\u4e0b\u6587\u521b\u5efa\u3001\u53d8\u91cf\u5bf9\u8c61\u751f\u6210\u548c\u6d3b\u52a8\u5bf9\u8c61\u6fc0\u6d3b\u7684\u8fc7\u7a0b<\/strong>\u3002\u5f53\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u65f6\uff0cJavaScript\u5f15\u64ce\u4f1a<strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u538b\u5165\u6267\u884c\u4e0a\u4e0b\u6587\u6808\u4e2d<\/strong>\uff0c\u63a5\u7740\u5bf9\u88ab\u8c03\u7528\u51fd\u6570\u8fdb\u884c\u6267\u884c\u524d\u7684\u51c6\u5907\u5de5\u4f5c\uff0c\u5305\u62ec\u53d8\u91cf\u5bf9\u8c61\uff08Variable Object\uff09\u7684\u521d\u59cb\u5316\u548c\u6d3b\u52a8\u5bf9\u8c61\uff08Activation Object\uff09\u7684\u5efa\u7acb\u3002\u8be6\u60c5\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u6808\uff08Call Stack\uff09\u548c\u4f5c\u7528\u57df\u94fe\uff08Scope Ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\uff09\u7684\u6982\u5ff5\u6765\u8fdb\u4e00\u6b65\u89e3\u91ca\uff0c\u8fd9\u4e24\u4e2a\u6982\u5ff5\u662f\u7406\u89e3\u5185\u5b58\u4e2d\u51fd\u6570\u8c03\u7528\u673a\u5236\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u8c03\u7528\u6808\u4e0e\u6267\u884c\u4e0a\u4e0b\u6587<\/p>\n<\/p>\n<p><p>\u8c03\u7528\u6808\uff08Call Stack\uff09\u662fJavaScript\u5f15\u64ce\u8ffd\u8e2a\u51fd\u6570\u6267\u884c\u6d41\u7a0b\u7684\u7ed3\u6784\u3002\u5f53\u4e00\u4e2a\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0cJavaScript\u5f15\u64ce\u4f1a\u4e3a\u8be5\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u6267\u884c\u4e0a\u4e0b\u6587\uff08Execution Context\uff09\uff0c\u5e76\u5c06\u5176\u63a8\u5165\u8c03\u7528\u6808\u9876\u90e8\u3002\u8fd9\u4e2a\u6267\u884c\u4e0a\u4e0b\u6587\u662f\u51fd\u6570\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u73af\u5883\uff0c\u5e76\u4e14\u5305\u542b\u4e86\u8be5\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3001\u53c2\u6570\u4ee5\u53ca\u5176\u4ed6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u6bcf\u4e2a\u6267\u884c\u4e0a\u4e0b\u6587\u5305\u62ec\u4e00\u4e2a\u53d8\u91cf\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5b58\u50a8\u7740\u51fd\u6570\u7684\u53c2\u6570\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u51fd\u6570\u58f0\u660e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6d3b\u52a8\u5bf9\u8c61\u4e0e\u53d8\u91cf\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u6267\u884c\u65f6\uff0c\u53d8\u91cf\u5bf9\u8c61\u4f1a\u53d8\u6210\u6d3b\u52a8\u5bf9\u8c61\uff08Activation Object\uff09\u3002\u6d3b\u52a8\u5bf9\u8c61\u5728\u51fd\u6570\u5f00\u59cb\u6267\u884c\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u5c06\u51fd\u6570\u7684\u53c2\u6570\u3001\u5185\u90e8\u53d8\u91cf\u4ee5\u53ca\u51fd\u6570\u672c\u8eab\u4f5c\u4e3a\u5176\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u5168\u5c40\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u53d8\u91cf\u5bf9\u8c61\u662f\u5168\u5c40\u5bf9\u8c61\u672c\u8eab<\/strong>\uff0c\u5b83\u5305\u62ec\u4e86\u5168\u5c40\u73af\u5883\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u5168\u5c40\u51fd\u6570\u3001\u5168\u5c40\u53d8\u91cf\u548c\u5168\u5c40\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u51fd\u6570\u8c03\u7528\u548c\u5185\u5b58\u5206\u914d<\/p>\n<\/p>\n<p><p>\u5f53\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u65f6\uff0c\u5185\u5b58\u4e2d\u4f1a\u4e3a\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u3002JavaScript\u5185\u5b58\u5206\u914d\u4e3b\u8981\u6d89\u53ca\u5806\uff08Heap\uff09\u548c\u6808\uff08Stack\uff09\u3002\u51fd\u6570\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u3001\u57fa\u672c\u7c7b\u578b\u7684\u6570\u636e\u5b58\u50a8\u5728\u6808\u4e0a\uff0c\u800c\u5bf9\u8c61\u7c7b\u578b\u7684\u6570\u636e\u5b58\u50a8\u5728\u5806\u4e0a\u3002<\/p>\n<\/p>\n<p><p><strong>\u88ab\u8c03\u7528\u51fd\u6570\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u4f1a\u5305\u542b\u4e00\u4e2a\u6307\u5411\u5f53\u524d\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u5f15\u7528\uff0c\u5f62\u6210\u4f5c\u7528\u57df\u94fe\uff0c\u4ee5\u4fbf\u8bbf\u95ee\u5916\u90e8\u53d8\u91cf\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f5c\u7528\u57df\u94fe<\/p>\n<\/p>\n<p><p>\u6bcf\u4e2a\u6267\u884c\u4e0a\u4e0b\u6587\u90fd\u6709\u4e00\u4e2a\u4e0e\u4e4b\u5173\u8054\u7684\u4f5c\u7528\u57df\u94fe\u3002\u4f5c\u7528\u57df\u94fe\u662f\u4e00\u7cfb\u5217\u5bf9\u8c61\u7684\u5217\u8868\uff0c\u7528\u4e8e\u89e3\u6790\u53d8\u91cf\u548c\u51fd\u6570\u7684\u6807\u8bc6\u7b26\u3002<strong>\u51fd\u6570\u521b\u5efa\u65f6\u7684\u4f5c\u7528\u57df\u94fe\uff0c\u4f1a\u88ab\u4fdd\u5b58\u5728\u5185\u90e8\u5c5e\u6027[[Scope]]\u4e2d\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5f53\u51fd\u6570\u6267\u884c\u65f6\uff0c\u5b83\u7684\u6d3b\u52a8\u5bf9\u8c61\u4f1a\u88ab\u63a8\u5165\u4f5c\u7528\u57df\u94fe\u7684\u524d\u7aef<\/strong>\uff0c\u8fd9\u6837\u5728\u51fd\u6570\u5185\u90e8\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5230\u5b9a\u4e49\u5728\u5916\u90e8\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5783\u573e\u56de\u6536\u673a\u5236<\/p>\n<\/p>\n<p><p>JavaScript\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u4f1a\u5b9a\u671f\u6e05\u7406\u90a3\u4e9b\u4e0d\u518d\u9700\u8981\u7684\u5bf9\u8c61\u548c\u51fd\u6570\u76f8\u5173\u7684\u5185\u5b58\u3002\u5f53\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\uff0c\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u5f15\u7528\u4f9d\u8d56\u5b83\u7684\u6267\u884c\u4e0a\u4e0b\u6587\uff0c\u90a3\u4e48JavaScript\u5f15\u64ce\u53ef\u4ee5\u56de\u6536\u8fd9\u90e8\u5206\u5185\u5b58\uff0c\u786e\u4fdd\u6709\u6548\u7684\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u5168\u5c40\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u4e0d\u4f1a\u7acb\u5373\u89e6\u53d1\u5783\u573e\u56de\u6536\uff0c\u4f46\u5f53\u8fd9\u4e9b\u51fd\u6570\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u51fa\u6808\u65f6\uff0c\u8fd9\u90e8\u5206\u5185\u5b58\u5c31\u53ef\u80fd\u88ab\u56de\u6536\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u7efc\u5408\u4e0a\u8ff0\u63cf\u8ff0\uff0c\u5168\u5c40\u51fd\u6570\u95f4\u7684\u8c03\u7528\u5728\u5185\u5b58\u4e2d\u8868\u73b0\u4e3a\u4e00\u7cfb\u5217\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u521b\u5efa\u3001\u6fc0\u6d3b\u548c\u9500\u6bc1\u8fc7\u7a0b\u3002\u8fd9\u4e9b\u8fc7\u7a0b\u662f\u5728JavaScript\u7684\u8c03\u7528\u6808\u548c\u5806\u6808\u4e2d\u8fdb\u884c\u7ba1\u7406\u7684\uff0c\u5e76\u901a\u8fc7\u4f5c\u7528\u57df\u94fe\u3001\u6d3b\u52a8\u5bf9\u8c61\u548c\u53d8\u91cf\u5bf9\u8c61\u7b49\u6982\u5ff5\u534f\u540c\u5de5\u4f5c\u3002\u7406\u89e3\u8fd9\u4e9b\u539f\u7406\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u7406\u89e3JavaScript\u7684\u51fd\u6570\u8c03\u7528\u673a\u5236\u4ee5\u53ca\u4e0e\u4e4b\u76f8\u5173\u7684\u5185\u5b58\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. JavaScript\u4e2d\u5168\u5c40\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u65f6\uff0c\u5185\u5b58\u662f\u5982\u4f55\u8fd0\u4f5c\u7684\uff1f<\/strong><\/p>\n<p>\u5168\u5c40\u51fd\u6570\u4e4b\u95f4\u7684\u8c03\u7528\uff0c\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u7684\u4e0a\u4e0b\u6587\u3002\u5f53\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u65f6\uff0c\u6267\u884c\u4e0a\u4e0b\u6587\u4f1a\u88ab\u538b\u5165\u8c03\u7528\u6808\u4e2d\uff0c\u5f53\u524d\u51fd\u6570\u7684\u6267\u884c\u88ab\u6682\u505c\uff0c\u7136\u540e\u6267\u884c\u88ab\u8c03\u7528\u7684\u5168\u5c40\u51fd\u6570\u3002\u5f53\u88ab\u8c03\u7528\u7684\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5b83\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u5c06\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\uff0c\u63a7\u5236\u6743\u8fd4\u56de\u7ed9\u539f\u59cb\u7684\u5168\u5c40\u51fd\u6570\uff0c\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p><strong>2. JavaScript\u4e2d\u5168\u5c40\u51fd\u6570\u4e4b\u95f4\u7684\u8c03\u7528\u662f\u5426\u4f1a\u5360\u7528\u989d\u5916\u7684\u5185\u5b58\u7a7a\u95f4\uff1f<\/strong><\/p>\n<p>JavaScript\u4e2d\u7684\u51fd\u6570\u8c03\u7528\u4f1a\u5728\u8c03\u7528\u6808\u4e2d\u5360\u7528\u4e00\u5b9a\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5305\u62ec\u4fdd\u5b58\u51fd\u6570\u7684\u53c2\u6570\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u6267\u884c\u4e0a\u4e0b\u6587\u7b49\u3002\u5f53\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u65f6\uff0c\u4f1a\u589e\u52a0\u8c03\u7528\u6808\u7684\u6df1\u5ea6\uff0c\u56e0\u6b64\u4f1a\u5360\u7528\u989d\u5916\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u5185\u5b58\u5360\u7528\u662f\u7531\u4e8e\u51fd\u6570\u7684\u9012\u5f52\u8c03\u7528\u6216\u6df1\u5ea6\u8c03\u7528\u9020\u6210\u7684\uff0c\u901a\u5e38\u4e0d\u4f1a\u5bf9\u6027\u80fd\u9020\u6210\u660e\u663e\u7684\u5f71\u54cd\u3002<\/p>\n<p><strong>3. \u5728 JavaScript \u4e2d\uff0c\u5982\u4f55\u89e3\u51b3\u5168\u5c40\u51fd\u6570\u4e4b\u95f4\u7684\u5faa\u73af\u8c03\u7528\u5f15\u53d1\u7684\u5185\u5b58\u5360\u7528\u95ee\u9898\uff1f<\/strong><\/p>\n<p>\u5faa\u73af\u8c03\u7528\u4e24\u4e2a\u5168\u5c40\u51fd\u6570\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u89e3\u51b3\uff1a<\/p>\n<ul>\n<li>\u5c3d\u91cf\u907f\u514d\u5168\u5c40\u51fd\u6570\u4e4b\u95f4\u7684\u5faa\u73af\u8c03\u7528\uff0c\u5c06\u4ee3\u7801\u903b\u8f91\u91cd\u65b0\u89c4\u5212\uff0c\u907f\u514d\u4ea7\u751f\u5faa\u73af\u8c03\u7528\u7684\u573a\u666f\u3002<\/li>\n<li>\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6216\u5faa\u73af\u8bed\u53e5\uff0c\u5c06\u8c03\u7528\u51fd\u6570\u7684\u903b\u8f91\u653e\u5728\u5408\u9002\u7684\u5730\u65b9\u8c03\u7528\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5728\u5168\u5c40\u51fd\u6570\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u3002<\/li>\n<li>\u5c06\u5168\u5c40\u51fd\u6570\u8c03\u7528\u6539\u4e3a\u5c40\u90e8\u51fd\u6570\u4e4b\u95f4\u7684\u8c03\u7528\uff0c\u901a\u8fc7\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u6765\u5b9e\u73b0\u76ee\u7684\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u907f\u514d\u5faa\u73af\u8c03\u7528\u6240\u5e26\u6765\u7684\u5185\u5b58\u5360\u7528\u95ee\u9898\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"JavaScript\u5728\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\u65f6\uff0c\u5185\u5b58\u5806\u6808\u4e2d\u4f1a\u53d1\u751f\u51fd\u6570\u8c03\u7528\u3001\u6267\u884c\u4e0a\u4e0b\u6587\u521b\u5efa\u3001\u53d8\u91cf\u5bf9\u8c61\u751f [&hellip;]","protected":false},"author":3,"featured_media":166397,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/166392"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=166392"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/166392\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/166397"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=166392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=166392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=166392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}