{"id":152,"date":"2024-12-10T23:36:02","date_gmt":"2024-12-10T15:36:02","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=152"},"modified":"2024-12-10T23:36:02","modified_gmt":"2024-12-10T15:36:02","slug":"%e5%a6%82%e4%bd%95%e5%87%8f%e5%b0%91%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f%e5%9c%a8-javascript-%e4%b8%ad%e7%9a%84%e5%8d%b1%e5%ae%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/10\/%e5%a6%82%e4%bd%95%e5%87%8f%e5%b0%91%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f%e5%9c%a8-javascript-%e4%b8%ad%e7%9a%84%e5%8d%b1%e5%ae%b3%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u5728 JavaScript \u4e2d\u7684\u5371\u5bb3\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u4e0a\u7bc7\u6587\u7ae0\u89e3\u91ca\u4e86\u5168\u5c40\u53d8\u91cf\u5728 JavaScript \u4e2d\u7684\u5371\u5bb3\uff0c\u672c\u7bc7\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u6765\u51cf\u5c11\u8fd9\u79cd\u5371\u5bb3\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u4ee3\u66ff\u5168\u5c40\u53d8\u91cf<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7 <code>let<\/code> \u6216 <code>const<\/code> \u58f0\u660e\u53d8\u91cf\uff0c\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u907f\u514d\u5b83\u4eec\u6cc4\u6f0f\u5230\u5168\u5c40\u73af\u5883\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   function doSomething() {\n       let localVar = \"This is local\"; \/\/ \u5c40\u90e8\u53d8\u91cf\n       console.log(localVar);\n   }\n   doSomething();\n   \/\/ console.log(localVar); \/\/ \u62a5\u9519\uff0clocalVar \u53ea\u5728\u51fd\u6570\u5185\u90e8\u6709\u6548<\/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>\u521b\u5efa\u547d\u540d\u7a7a\u95f4<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06\u53d8\u91cf\u548c\u65b9\u6cd5\u5c01\u88c5\u5230\u4e00\u4e2a\u5bf9\u8c61\u4e2d\uff0c\u51cf\u5c11\u76f4\u63a5\u5411\u5168\u5c40\u4f5c\u7528\u57df\u6dfb\u52a0\u53d8\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   const MyApp = {}; \/\/ \u521b\u5efa\u547d\u540d\u7a7a\u95f4\n   MyApp.config = {\n       apiUrl: \"https:\/\/example.com\/api\",\n       version: \"1.0.0\"\n   };\n   MyApp.doSomething = function() {\n       console.log(\"Doing something!\");\n   };\n   MyApp.doSomething();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u3002<\/li>\n\n\n\n<li>\u65b9\u4fbf\u7ba1\u7406\u53d8\u91cf\u548c\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528\u95ed\u5305<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7 IIFE\uff08\u7acb\u5373\u8c03\u7528\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\uff09\u6216\u95ed\u5305\u5c06\u53d8\u91cf\u5c01\u88c5\u5728\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\uff0c\u907f\u514d\u6c61\u67d3\u5168\u5c40\u73af\u5883\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   (function() {\n       let privateVar = \"This is private\"; \/\/ \u79c1\u6709\u53d8\u91cf\n       function privateFunction() {\n           console.log(privateVar);\n       }\n       privateFunction();\n   })();\n   \/\/ console.log(privateVar); \/\/ \u62a5\u9519\uff0c\u53d8\u91cf\u672a\u66b4\u9732\u5230\u5168\u5c40<\/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>\u4f7f\u7528\u6a21\u5757\u5316<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u73b0\u4ee3 JavaScript \u4e2d\uff0c\u6a21\u5757\u5316\u662f\u4e00\u79cd\u975e\u5e38\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u5b83\u5c06\u53d8\u91cf\u548c\u903b\u8f91\u5c01\u88c5\u5728\u6a21\u5757\u5185\u90e8\u3002<\/li>\n\n\n\n<li><strong>ES Modules<\/strong> <code>\/\/ module.js export const myVar = \"Hello, Module!\"; export function myFunction() { console.log(myVar); } \/\/ main.js import { myVar, myFunction } from '.\/module.js'; console.log(myVar); \/\/ \"Hello, Module!\" myFunction();<\/code><\/li>\n\n\n\n<li><strong>CommonJS\uff08Node.js \u73af\u5883\uff09<\/strong> <code>\/\/ module.js const myVar = \"Hello, Module!\"; module.exports = { myVar, }; \/\/ main.js const { myVar } = require('.\/module'); console.log(myVar); \/\/ \"Hello, Module!\"<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u907f\u514d\u9690\u5f0f\u5168\u5c40\u53d8\u91cf<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u672a\u58f0\u660e\u53d8\u91cf\u65f6\u76f4\u63a5\u8d4b\u503c\u4f1a\u521b\u5efa\u9690\u5f0f\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u662f\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u793a\u4f8b<\/strong>\uff1a<br><code>javascript function test() { globalVar = \"This is global\"; \/\/ \u672a\u7528 let\/const\/var \u58f0\u660e } test(); console.log(globalVar); \/\/ \u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df<\/code><\/li>\n\n\n\n<li><strong>\u6b63\u786e\u793a\u4f8b<\/strong>\uff1a<br><code>javascript function test() { let localVar = \"This is local\"; \/\/ \u4f7f\u7528 let \u6216 const \u58f0\u660e console.log(localVar); } test();<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u4f7f\u7528\u4e25\u683c\u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u542f\u7528\u4e25\u683c\u6a21\u5f0f (<code>\"use strict\";<\/code>) \u53ef\u4ee5\u7981\u6b62\u9690\u5f0f\u5168\u5c40\u53d8\u91cf\u7684\u521b\u5efa\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   \"use strict\";\n   function test() {\n       globalVar = \"This will throw an error\"; \/\/ \u62a5\u9519\n   }<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u4f7f\u7528\u5168\u5c40\u5bf9\u8c61\u4ee3\u66ff\u76f4\u63a5\u58f0\u660e<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u786e\u5b9e\u9700\u8981\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u663e\u5f0f\u5730\u6302\u8f7d\u5230 <code>window<\/code>\uff08\u6d4f\u89c8\u5668\u73af\u5883\uff09\u6216 <code>global<\/code>\uff08Node.js \u73af\u5883\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   window.myGlobal = \"I am global\";\n   console.log(window.myGlobal); \/\/ \"I am global\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u5de5\u5177\u5316\u68c0\u6d4b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u4ee3\u7801\u5206\u6790\u5de5\u5177\uff08\u5982 ESLint\uff09\uff0c\u901a\u8fc7\u89c4\u5219\u6765\u68c0\u6d4b\u548c\u9632\u6b62\u5168\u5c40\u53d8\u91cf\u7684\u6ee5\u7528\u3002\n<ul class=\"wp-block-list\">\n<li><strong>\u914d\u7f6e ESLint \u89c4\u5219<\/strong>\uff1a<\/li>\n\n\n\n<li><code>no-undef<\/code>: \u68c0\u6d4b\u672a\u58f0\u660e\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><code>no-global-assign<\/code>: \u7981\u6b62\u4fee\u6539\u5168\u5c40\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9996\u9009<\/strong>\uff1a\u5c40\u90e8\u53d8\u91cf\u3001\u6a21\u5757\u5316\u548c\u95ed\u5305\u3002<\/li>\n\n\n\n<li><strong>\u6b21\u9009<\/strong>\uff1a\u547d\u540d\u7a7a\u95f4\u548c\u663e\u5f0f\u6302\u8f7d\u5230\u5168\u5c40\u5bf9\u8c61\uff08\u4ec5\u5728\u5fc5\u8981\u65f6\uff09\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7\u4e25\u683c\u6a21\u5f0f\u548c\u5de5\u5177\u68c0\u6d4b\uff0c\u51cf\u5c11\u9690\u5f0f\u5168\u5c40\u53d8\u91cf\u7684\u98ce\u9669\uff0c\u4ece\u4ee3\u7801\u4e60\u60ef\u4e0a\u675c\u7edd\u5168\u5c40\u6c61\u67d3\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u662f\u9488\u5bf9\u5982\u4f55\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u5728 JavaScript \u4e2d\u7684\u5371\u5bb3\u7684\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\uff0c\u66f4\u591a\u4fe1\u606f\u8bf7\u5173\u6ce8\u672c\u7ad9\u5176\u4ed6\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u7bc7\u6587\u7ae0\u89e3\u91ca\u4e86\u5168\u5c40\u53d8\u91cf\u5728 JavaScript \u4e2d\u7684\u5371\u5bb3\uff0c\u672c\u7bc7\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u6765\u51cf\u5c11\u8fd9\u79cd\u5371\u5bb3\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f [&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-152","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/152","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=152"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"predecessor-version":[{"id":153,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/152\/revisions\/153"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}