{"id":2017,"date":"2025-02-25T22:25:34","date_gmt":"2025-02-25T14:25:34","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2017"},"modified":"2025-02-25T22:25:34","modified_gmt":"2025-02-25T14:25:34","slug":"javascript-%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/25\/javascript-%e5%8f%98%e9%87%8f\/","title":{"rendered":"JavaScript \u53d8\u91cf"},"content":{"rendered":"\n<p>\u5728 JavaScript \u4e2d\uff0c\u53d8\u91cf\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u503c\u3002\u4f60\u53ef\u4ee5\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\uff0c\u5982\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u3001\u5bf9\u8c61\u7b49\u3002\u53d8\u91cf\u901a\u8fc7\u6807\u8bc6\u7b26\uff08\u540d\u5b57\uff09\u8fdb\u884c\u8bbf\u95ee\u548c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u58f0\u660e\u53d8\u91cf<\/strong><\/h3>\n\n\n\n<p>JavaScript \u63d0\u4f9b\u4e86\u4e09\u79cd\u5173\u952e\u5b57\u6765\u58f0\u660e\u53d8\u91cf\uff1a<code>var<\/code>\u3001<code>let<\/code> \u548c <code>const<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a. <strong><code>var<\/code><\/strong> \u58f0\u660e\uff08\u4f20\u7edf\u65b9\u5f0f\uff09<\/h4>\n\n\n\n<p><code>var<\/code> \u662f JavaScript \u4e2d\u6700\u65e9\u7684\u53d8\u91cf\u58f0\u660e\u65b9\u5f0f\uff0c\u4f46\u5b83\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\uff08\u4f8b\u5982\u4f5c\u7528\u57df\u4e0d\u591f\u7cbe\u786e\uff09\u3002\u4ece ES6 \u8d77\uff0c<code>var<\/code> \u4e0d\u518d\u63a8\u8350\u4f7f\u7528\uff0c\u5efa\u8bae\u4f7f\u7528 <code>let<\/code> \u548c <code>const<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var name = \"Alice\";  \/\/ \u58f0\u660e\u53d8\u91cf\u5e76\u521d\u59cb\u5316\nconsole.log(name);  \/\/ \u8f93\u51fa\uff1aAlice<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">b. <strong><code>let<\/code><\/strong> \u58f0\u660e\uff08\u5757\u7ea7\u4f5c\u7528\u57df\uff09<\/h4>\n\n\n\n<p><code>let<\/code> \u662f ES6 \u5f15\u5165\u7684\u7528\u4e8e\u58f0\u660e\u53d8\u91cf\u7684\u5173\u952e\u5b57\u3002\u5b83\u6709\u5757\u7ea7\u4f5c\u7528\u57df\uff08\u5373\u53ea\u5728\u6240\u5728\u7684\u4ee3\u7801\u5757\u4e2d\u6709\u6548\uff09\uff0c\u89e3\u51b3\u4e86 <code>var<\/code> \u7684\u4e00\u4e9b\u4f5c\u7528\u57df\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let age = 25;  \/\/ \u58f0\u660e\u4e00\u4e2a\u53d8\u91cf age\nage = 30;  \/\/ \u66f4\u65b0\u53d8\u91cf\u503c\nconsole.log(age);  \/\/ \u8f93\u51fa\uff1a30<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">c. <strong><code>const<\/code><\/strong> \u58f0\u660e\uff08\u5e38\u91cf\uff09<\/h4>\n\n\n\n<p><code>const<\/code> \u4e5f\u662f ES6 \u5f15\u5165\u7684\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u58f0\u660e\u5e38\u91cf\u3002\u5e38\u91cf\u4e00\u65e6\u58f0\u660e\u5e76\u521d\u59cb\u5316\u540e\uff0c\u5176\u503c\u4e0d\u80fd\u518d\u6539\u53d8\u3002<code>const<\/code> \u4e5f\u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const pi = 3.14159;  \/\/ \u58f0\u660e\u5e38\u91cf\n\/\/ pi = 3.14;  \/\/ \u4f1a\u62a5\u9519\uff1aAssignment to constant variable.\nconsole.log(pi);  \/\/ \u8f93\u51fa\uff1a3.14159<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<code>const<\/code> \u53ea\u80fd\u5728\u58f0\u660e\u65f6\u521d\u59cb\u5316\uff0c\u4e14\u4e0d\u80fd\u91cd\u65b0\u8d4b\u503c\u3002\u4f46\u662f\uff0c\u5982\u679c\u53d8\u91cf\u662f\u5bf9\u8c61\u6216\u6570\u7ec4\uff0c<strong>\u5bf9\u8c61\u7684\u5c5e\u6027\u6216\u6570\u7ec4\u7684\u5143\u7d20<\/strong>\u662f\u53ef\u4ee5\u4fee\u6539\u7684\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>const person = { name: \"Alice\", age: 25 };\nperson.age = 26;  \/\/ \u53ef\u4ee5\u4fee\u6539\u5bf9\u8c61\u7684\u5c5e\u6027\nconsole.log(person);  \/\/ \u8f93\u51fa\uff1a{ name: \"Alice\", age: 26 }<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u53d8\u91cf\u547d\u540d\u89c4\u5219<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53d8\u91cf\u540d\u53ef\u4ee5\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf (<code>_<\/code>) \u548c\u7f8e\u5143\u7b26\u53f7 (<code>$<\/code>)\u3002<\/li>\n\n\n\n<li>\u53d8\u91cf\u540d\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002<\/li>\n\n\n\n<li>\u53d8\u91cf\u540d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u5373 <code>age<\/code> \u548c <code>Age<\/code> \u88ab\u8ba4\u4e3a\u662f\u4e0d\u540c\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li>\u4e0d\u80fd\u4f7f\u7528 JavaScript \u7684\u4fdd\u7559\u5b57\u6216\u5173\u952e\u5b57\uff08\u5982 <code>let<\/code>\u3001<code>if<\/code>\u3001<code>for<\/code> \u7b49\uff09\u4f5c\u4e3a\u53d8\u91cf\u540d\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u5408\u6cd5\u7684\u53d8\u91cf\u540d\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let name = \"Alice\";\nlet _score = 95;\nlet $price = 100;\nlet userAge = 30;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e0d\u5408\u6cd5\u7684\u53d8\u91cf\u540d\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let 1stName = \"John\";  \/\/ \u9519\u8bef\uff0c\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\nlet let = 10;  \/\/ \u9519\u8bef\uff0c\"let\" \u662f JavaScript \u7684\u5173\u952e\u5b57<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u53d8\u91cf\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>JavaScript \u53d8\u91cf\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u57fa\u672c\u7c7b\u578b<\/strong>\uff1a\u6570\u5b57\uff08<code>number<\/code>\uff09\u3001\u5b57\u7b26\u4e32\uff08<code>string<\/code>\uff09\u3001\u5e03\u5c14\u503c\uff08<code>boolean<\/code>\uff09\u3001<code>undefined<\/code>\u3001<code>null<\/code>\u3001\u7b26\u53f7\uff08<code>symbol<\/code>\uff09\u3001\u5927\u6574\u6570\uff08<code>bigint<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5f15\u7528\u7c7b\u578b<\/strong>\uff1a\u5bf9\u8c61\uff08<code>object<\/code>\uff09\u3001\u6570\u7ec4\uff08<code>array<\/code>\uff09\u3001\u51fd\u6570\uff08<code>function<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let num = 100;  \/\/ \u6570\u5b57\u7c7b\u578b\nlet str = \"Hello, World!\";  \/\/ \u5b57\u7b26\u4e32\u7c7b\u578b\nlet isActive = true;  \/\/ \u5e03\u5c14\u7c7b\u578b\nlet person = { name: \"Alice\", age: 25 };  \/\/ \u5bf9\u8c61\u7c7b\u578b\nlet colors = &#91;\"red\", \"green\", \"blue\"];  \/\/ \u6570\u7ec4\u7c7b\u578b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u53d8\u91cf\u4f5c\u7528\u57df<\/strong><\/h3>\n\n\n\n<p>\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u51b3\u5b9a\u4e86\u5b83\u5728\u7a0b\u5e8f\u4e2d\u7684\u53ef\u89c1\u6027\u548c\u751f\u547d\u5468\u671f\u3002JavaScript \u53d8\u91cf\u7684\u4f5c\u7528\u57df\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a. <strong>\u5168\u5c40\u4f5c\u7528\u57df<\/strong><\/h4>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u53d8\u91cf\u5728\u51fd\u6570\u4e4b\u5916\u58f0\u660e\u65f6\uff0c\u5b83\u5c31\u62e5\u6709\u4e86\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u5730\u65b9\u8bbf\u95ee\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let globalVar = \"I am global\";  \/\/ \u5168\u5c40\u53d8\u91cf\nfunction display() {\n    console.log(globalVar);  \/\/ \u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\n}\ndisplay();  \/\/ \u8f93\u51fa\uff1aI am global<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">b. <strong>\u51fd\u6570\u4f5c\u7528\u57df<\/strong><\/h4>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u53d8\u91cf\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u65f6\uff0c\u5b83\u4ec5\u5728\u8be5\u51fd\u6570\u5185\u6709\u6548\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function greet() {\n    let localVar = \"I am local\";  \/\/ \u5c40\u90e8\u53d8\u91cf\n    console.log(localVar);  \/\/ \u53ef\u4ee5\u8bbf\u95ee\u5c40\u90e8\u53d8\u91cf\n}\ngreet();  \/\/ \u8f93\u51fa\uff1aI am local\n\/\/ console.log(localVar);  \/\/ \u9519\u8bef\uff0clocalVar \u5728\u51fd\u6570\u5916\u4e0d\u53ef\u8bbf\u95ee<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">c. <strong>\u5757\u7ea7\u4f5c\u7528\u57df<\/strong><\/h4>\n\n\n\n<p><code>let<\/code> \u548c <code>const<\/code> \u58f0\u660e\u7684\u53d8\u91cf\u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df\uff0c\u5373\u5b83\u4eec\u53ea\u5728\u6240\u58f0\u660e\u7684\u4ee3\u7801\u5757\u4e2d\u6709\u6548\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (true) {\n    let blockVar = \"I am inside block\";  \/\/ \u5757\u7ea7\u53d8\u91cf\n    console.log(blockVar);  \/\/ \u53ef\u4ee5\u8bbf\u95ee\u5757\u7ea7\u53d8\u91cf\n}\n\/\/ console.log(blockVar);  \/\/ \u9519\u8bef\uff0cblockVar \u5728\u5757\u5916\u4e0d\u53ef\u8bbf\u95ee<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u53d8\u91cf\u63d0\u5347\uff08Hoisting\uff09<\/strong><\/h3>\n\n\n\n<p>\u5728 JavaScript \u4e2d\uff0c\u53d8\u91cf\u58f0\u660e\u4f1a\u88ab\u63d0\u5347\u5230\u51fd\u6570\u6216\u4ee3\u7801\u5757\u7684\u9876\u90e8\u3002\u7136\u800c\uff0c<strong>\u53ea\u6709\u58f0\u660e\u4f1a\u88ab\u63d0\u5347<\/strong>\uff0c\u800c\u8d4b\u503c\u5219\u4e0d\u4f1a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(x);  \/\/ \u8f93\u51fa\uff1aundefined\nvar x = 10;\nconsole.log(x);  \/\/ \u8f93\u51fa\uff1a10<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf <code>x<\/code> \u7684\u58f0\u660e\u88ab\u63d0\u5347\u5230\u4e86\u9876\u90e8\uff0c\u4f46\u662f\u5b83\u7684\u8d4b\u503c <code>10<\/code> \u4fdd\u6301\u5728\u539f\u4f4d\u7f6e\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u8f93\u51fa\u65f6\u662f <code>undefined<\/code>\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e <code>let<\/code> \u548c <code>const<\/code> \u58f0\u660e\u7684\u53d8\u91cf\uff0c\u867d\u7136\u5b83\u4eec\u4e5f\u4f1a\u88ab\u63d0\u5347\uff0c\u4f46\u5728\u58f0\u660e\u4e4b\u524d\u65e0\u6cd5\u8bbf\u95ee\uff0c\u5bfc\u81f4\u201c\u6682\u65f6\u6027\u6b7b\u533a\u201d\uff08Temporal Dead Zone\uff0cTDZ\uff09\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(y);  \/\/ \u9519\u8bef\uff0cReferenceError: Cannot access 'y' before initialization\nlet y = 20;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c0f\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>var<\/code><\/strong>\u3001<strong><code>let<\/code><\/strong> \u548c <strong><code>const<\/code><\/strong> \u90fd\u53ef\u4ee5\u7528\u4e8e\u58f0\u660e\u53d8\u91cf\uff0c\u5176\u4e2d <code>let<\/code> \u548c <code>const<\/code> \u662f\u73b0\u4ee3 JavaScript \u4e2d\u63a8\u8350\u4f7f\u7528\u7684\u58f0\u660e\u65b9\u5f0f\uff0c<code>const<\/code> \u7528\u4e8e\u5e38\u91cf\uff0c<code>let<\/code> \u7528\u4e8e\u53ef\u53d8\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u53d8\u91cf\u4f5c\u7528\u57df<\/strong>\uff1a<code>var<\/code> \u662f\u51fd\u6570\u4f5c\u7528\u57df\uff0c<code>let<\/code> \u548c <code>const<\/code> \u662f\u5757\u7ea7\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><strong>\u53d8\u91cf\u63d0\u5347<\/strong>\uff1aJavaScript \u4f1a\u5c06\u53d8\u91cf\u58f0\u660e\u63d0\u5347\u5230\u4ee3\u7801\u9876\u90e8\uff0c\u4f46\u8d4b\u503c\u4e0d\u4f1a\u63d0\u5347\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0c\u53d8\u91cf\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u503c\u3002\u4f60\u53ef\u4ee5\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\uff0c\u5982\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2017","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2017"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2017\/revisions"}],"predecessor-version":[{"id":2018,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2017\/revisions\/2018"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}