{"id":2063,"date":"2025-02-26T23:24:07","date_gmt":"2025-02-26T15:24:07","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2063"},"modified":"2025-02-26T23:24:07","modified_gmt":"2025-02-26T15:24:07","slug":"javascript-%e4%bd%bf%e7%94%a8%e8%af%af%e5%8c%ba","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-%e4%bd%bf%e7%94%a8%e8%af%af%e5%8c%ba\/","title":{"rendered":"JavaScript \u4f7f\u7528\u8bef\u533a"},"content":{"rendered":"\n<p>\u5728\u5b66\u4e60\u548c\u4f7f\u7528 JavaScript \u65f6\uff0c\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u8bef\u533a\u3002\u7406\u89e3\u8fd9\u4e9b\u8bef\u533a\u53ef\u4ee5\u5e2e\u52a9\u4f60\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u7f16\u5199\u66f4\u52a0\u53ef\u9760\u548c\u9ad8\u6548\u7684\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684 JavaScript \u4f7f\u7528\u8bef\u533a\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u5ffd\u7565\u7c7b\u578b\u8f6c\u6362\u548c\u6bd4\u8f83\u7684\u5dee\u5f02<\/strong><\/h3>\n\n\n\n<p>JavaScript \u7684\u7c7b\u578b\u8f6c\u6362\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u610f\u5916\u7684\u884c\u4e3a\u3002\u7279\u522b\u662f\u5728\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c<code>==<\/code> \u548c <code>===<\/code> \u8fd0\u7b97\u7b26\u7684\u5dee\u5f02\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>==<\/code> \u4f1a\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u800c <code>===<\/code> \u4e0d\u4f1a\u3002\u4f8b\u5982\uff0c<code>0 == '0'<\/code> \u4f1a\u8fd4\u56de <code>true<\/code>\uff0c\u800c <code>0 === '0'<\/code> \u4f1a\u8fd4\u56de <code>false<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(0 == '0');    \/\/ true\nconsole.log(0 === '0');   \/\/ false<\/code><\/pre>\n\n\n\n<p><strong>\u8bef\u533a\uff1a<\/strong> \u7531\u4e8e <code>==<\/code> \u4f1a\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u5bb9\u6613\u4ea7\u751f\u4e0d\u7b26\u5408\u9884\u671f\u7684\u7ed3\u679c\u3002\u4f7f\u7528 <code>===<\/code> \u6765\u8fdb\u884c\u4e25\u683c\u6bd4\u8f83\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528 <code>var<\/code> \u58f0\u660e\u53d8\u91cf\uff0c\u5bfc\u81f4\u53d8\u91cf\u63d0\u5347\u95ee\u9898<\/strong><\/h3>\n\n\n\n<p>\u5728 JavaScript \u4e2d\uff0c\u4f7f\u7528 <code>var<\/code> \u58f0\u660e\u7684\u53d8\u91cf\u4f1a\u88ab\u63d0\u5347\u5230\u5f53\u524d\u4f5c\u7528\u57df\u7684\u9876\u90e8\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u53ef\u9884\u671f\u7684\u7ed3\u679c\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u63a8\u8350\u4f7f\u7528 <code>let<\/code> \u548c <code>const<\/code>\uff0c\u5b83\u4eec\u5728\u5757\u7ea7\u4f5c\u7528\u57df\u5185\u58f0\u660e\u53d8\u91cf\uff0c\u4e0d\u4f1a\u88ab\u63d0\u5347\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(x); \/\/ undefined\nvar x = 5;<\/code><\/pre>\n\n\n\n<p><strong>\u8bef\u533a\uff1a<\/strong> \u7531\u4e8e\u53d8\u91cf\u58f0\u660e\u4f1a\u88ab\u63d0\u5347\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u6df7\u4e71\u3002\u4f7f\u7528 <code>let<\/code> \u548c <code>const<\/code> \u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u5ffd\u7565\u5f02\u6b65\u64cd\u4f5c\u7684\u56de\u8c03<\/strong><\/h3>\n\n\n\n<p>JavaScript \u4e2d\u7684\u5f02\u6b65\u64cd\u4f5c\uff08\u5982 <code>setTimeout<\/code>\u3001<code>fetch<\/code> \u7b49\uff09\u53ef\u80fd\u4f1a\u8ba9\u4ee3\u7801\u5728\u6267\u884c\u65f6\u4ea7\u751f\u4e0d\u53ef\u9884\u671f\u7684\u987a\u5e8f\u3002\u5c24\u5176\u662f\u5728\u5904\u7406\u5f02\u6b65\u51fd\u6570\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u6b63\u786e\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u3001<code>Promises<\/code> \u6216 <code>async\/await<\/code>\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u56de\u8c03\u5730\u72f1\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>setTimeout(function() {\n  console.log(\"Hello\");\n}, 1000);\n\nconsole.log(\"World\");<\/code><\/pre>\n\n\n\n<p><strong>\u8bef\u533a\uff1a<\/strong> \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\u987a\u5e8f\u53ef\u80fd\u4f1a\u8ba9\u4f60\u611f\u5230\u56f0\u60d1\u3002\u6b63\u786e\u7684\u5f02\u6b65\u5904\u7406\u65b9\u6cd5\u662f\u4f7f\u7528 <code>Promises<\/code> \u6216 <code>async\/await<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u4f7f\u7528 <code>this<\/code> \u65f6\u7684\u4e0a\u4e0b\u6587\u95ee\u9898<\/strong><\/h3>\n\n\n\n<p>\u5728 JavaScript \u4e2d\uff0c<code>this<\/code> \u5173\u952e\u5b57\u7684\u503c\u662f\u7531\u51fd\u6570\u7684\u8c03\u7528\u65b9\u5f0f\u51b3\u5b9a\u7684\uff0c\u800c\u4e0d\u662f\u7531\u51fd\u6570\u672c\u8eab\u51b3\u5b9a\u7684\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4f60\u5728\u4e8b\u4ef6\u5904\u7406\u5668\u6216\u56de\u8c03\u51fd\u6570\u4e2d\u9047\u5230 <code>this<\/code> \u6307\u5411\u4e0d\u6b63\u786e\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>function myFunction() {\n  console.log(this);  \/\/ `this` \u7684\u503c\u53d6\u51b3\u4e8e\u8c03\u7528\u65b9\u5f0f\n}\nmyFunction();  \/\/ `this` \u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff08\u5728\u6d4f\u89c8\u5668\u4e2d\u662f `window`\uff09<\/code><\/pre>\n\n\n\n<p><strong>\u8bef\u533a\uff1a<\/strong> \u5982\u679c\u4f60\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u4f7f\u7528 <code>this<\/code>\uff0c\u5b83\u53ef\u80fd\u4e0d\u4f1a\u6307\u5411\u4f60\u9884\u671f\u7684\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528 <code>bind()<\/code>\u3001<code>call()<\/code> \u6216 <code>apply()<\/code> \u6765\u663e\u5f0f\u7ed1\u5b9a <code>this<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u6ca1\u6709\u5904\u7406\u5bf9\u8c61\u548c\u6570\u7ec4\u7684\u5f15\u7528<\/strong><\/h3>\n\n\n\n<p>\u5728 JavaScript \u4e2d\uff0c\u5bf9\u8c61\u548c\u6570\u7ec4\u662f\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7684\uff0c\u800c\u4e0d\u662f\u503c\u4f20\u9012\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5f53\u4f60\u4fee\u6539\u5bf9\u8c61\u6216\u6570\u7ec4\u65f6\uff0c\u539f\u59cb\u5bf9\u8c61\u6216\u6570\u7ec4\u4e5f\u4f1a\u88ab\u4fee\u6539\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>let obj1 = { name: 'Alice' };\nlet obj2 = obj1;\nobj2.name = 'Bob';\n\nconsole.log(obj1.name);  \/\/ 'Bob'\nconsole.log(obj2.name);  \/\/ 'Bob'<\/code><\/pre>\n\n\n\n<p><strong>\u8bef\u533a\uff1a<\/strong> \u5982\u679c\u4f60\u5e0c\u671b\u590d\u5236\u5bf9\u8c61\u6216\u6570\u7ec4\u7684\u5185\u5bb9\uff0c\u5e94\u8be5\u4f7f\u7528\u6df1\u62f7\u8d1d\u800c\u4e0d\u662f\u6d45\u62f7\u8d1d\u3002\u53ef\u4ee5\u4f7f\u7528 <code>Object.assign()<\/code> \u6216\u5e93\u5982 Lodash \u7684 <code>cloneDeep()<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/strong><\/h3>\n\n\n\n<p>\u5728 JavaScript \u4e2d\uff0c\u7279\u522b\u662f\u5f53\u4f60\u4f7f\u7528 <code>var<\/code> \u58f0\u660e\u53d8\u91cf\u65f6\uff0c\u5f88\u5bb9\u6613\u4e0d\u5c0f\u5fc3\u521b\u5efa\u5168\u5c40\u53d8\u91cf\u3002\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u3001\u4ee3\u7801\u4e0d\u53ef\u7ef4\u62a4\u548c\u5176\u4ed6\u95ee\u9898\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>var globalVar = \"I am global!\";<\/code><\/pre>\n\n\n\n<p><strong>\u8bef\u533a\uff1a<\/strong> \u5c3d\u91cf\u907f\u514d\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49\u53d8\u91cf\u3002\u4f7f\u7528 <code>let<\/code> \u548c <code>const<\/code> \u58f0\u660e\u53d8\u91cf\uff0c\u5e76\u5c06\u5b83\u4eec\u5c01\u88c5\u5728\u51fd\u6570\u6216\u6a21\u5757\u4e2d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. \u5ffd\u89c6\u6d4f\u89c8\u5668\u517c\u5bb9\u6027<\/strong><\/h3>\n\n\n\n<p>\u4e0d\u540c\u6d4f\u89c8\u5668\u5bf9 JavaScript \u7684\u652f\u6301\u7a0b\u5ea6\u4e0d\u540c\u3002\u5982\u679c\u4e0d\u5173\u6ce8\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u5728\u67d0\u4e9b\u6d4f\u89c8\u5668\u4e2d\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002\u786e\u4fdd\u4f60\u68c0\u67e5\u517c\u5bb9\u6027\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u65b0\u7684 JavaScript \u7279\u6027\u65f6\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u8bef\u533a\uff1a<\/strong> \u4f7f\u7528\u4e0d\u517c\u5bb9\u7684 JavaScript \u7279\u6027\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u5728\u67d0\u4e9b\u6d4f\u89c8\u5668\u4e2d\u65e0\u6cd5\u8fd0\u884c\u3002\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982 Babel \u548c polyfills \u6765\u4fdd\u8bc1\u4ee3\u7801\u7684\u517c\u5bb9\u6027\u3002<\/h5>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. \u6ca1\u6709\u6b63\u786e\u7406\u89e3\u4e8b\u4ef6\u7684\u5192\u6ce1\u548c\u6355\u83b7\u673a\u5236<\/strong><\/h3>\n\n\n\n<p>\u4e8b\u4ef6\u7684\u5192\u6ce1\u548c\u6355\u83b7\u673a\u5236\u51b3\u5b9a\u4e86\u4e8b\u4ef6\u662f\u5982\u4f55\u5728 DOM \u6811\u4e2d\u4f20\u64ad\u7684\u3002\u5982\u679c\u4f60\u4e0d\u7406\u89e3\u4e8b\u4ef6\u7684\u4f20\u64ad\u987a\u5e8f\uff0c\u53ef\u80fd\u4f1a\u8bef\u7528\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u5bfc\u81f4\u4e0d\u7b26\u5408\u9884\u671f\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>document.getElementById('parent').addEventListener('click', function() {\n  alert(\"Parent clicked\");\n}, false); \/\/ \u6355\u83b7\u9636\u6bb5\n\ndocument.getElementById('child').addEventListener('click', function() {\n  alert(\"Child clicked\");\n}, true); \/\/ \u5192\u6ce1\u9636\u6bb5<\/code><\/pre>\n\n\n\n<p><strong>\u8bef\u533a\uff1a<\/strong> \u4e86\u89e3\u4e8b\u4ef6\u7684\u4f20\u64ad\u673a\u5236\uff0c\u5e76\u6839\u636e\u9700\u8981\u9009\u62e9\u4f7f\u7528\u6355\u83b7\u6216\u5192\u6ce1\u9636\u6bb5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. \u8bef\u7528\u6570\u7ec4\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p>JavaScript \u63d0\u4f9b\u4e86\u5f88\u591a\u6570\u7ec4\u65b9\u6cd5\uff0c\u5982 <code>map()<\/code>\u3001<code>filter()<\/code>\u3001<code>reduce()<\/code> \u7b49\uff0c\u4f46\u6709\u65f6\u5b83\u4eec\u88ab\u9519\u8bef\u5730\u4f7f\u7528\uff0c\u5c24\u5176\u662f\u5728\u4e0d\u7406\u89e3\u5176\u8fd4\u56de\u503c\u548c\u526f\u4f5c\u7528\u65f6\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>let arr = &#91;1, 2, 3];\nlet result = arr.map(function(n) {\n  return n * 2;\n});\n\nconsole.log(result);  \/\/ &#91;2, 4, 6]<\/code><\/pre>\n\n\n\n<p><strong>\u8bef\u533a\uff1a<\/strong> \u4e86\u89e3\u6bcf\u4e2a\u6570\u7ec4\u65b9\u6cd5\u7684\u884c\u4e3a\u53ca\u5176\u8fd4\u56de\u503c\uff0c\u907f\u514d\u8bef\u7528\u6570\u7ec4\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p>\u907f\u514d JavaScript \u4f7f\u7528\u8bef\u533a\u662f\u7f16\u5199\u9ad8\u8d28\u91cf\u4ee3\u7801\u7684\u91cd\u8981\u4e00\u73af\u3002\u901a\u8fc7\u7406\u89e3\u548c\u907f\u514d\u5e38\u89c1\u7684\u8bef\u533a\uff0c\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1 JavaScript\uff0c\u7f16\u5199\u66f4\u5065\u58ee\u3001\u53ef\u9760\u548c\u9ad8\u6548\u7684\u4ee3\u7801\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b66\u4e60\u548c\u4f7f\u7528 JavaScript \u65f6\uff0c\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u8bef\u533a\u3002\u7406\u89e3\u8fd9\u4e9b\u8bef\u533a\u53ef\u4ee5\u5e2e\u52a9\u4f60\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\uff0c [&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-2063","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2063","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=2063"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2063\/revisions"}],"predecessor-version":[{"id":2064,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2063\/revisions\/2064"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}