{"id":1380,"date":"2024-11-10T15:48:58","date_gmt":"2024-11-10T07:48:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1380\/"},"modified":"2024-11-10T15:48:58","modified_gmt":"2024-11-10T07:48:58","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-javascript-%e4%b8%ad%e7%bc%96%e5%86%99%e6%b8%85%e6%99%b0%e6%9c%89%e6%95%88%e7%9a%84%e4%bb%a3%e7%a0%81%e6%b3%a8%e9%87%8a%e5%b9%b6%e6%8f%90%e4%be%9b%e6%9b%b4%e5%a5%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1380\/","title":{"rendered":"\u5982\u4f55\u5728 JavaScript \u4e2d\u7f16\u5199\u6e05\u6670\u6709\u6548\u7684\u4ee3\u7801\u6ce8\u91ca\u5e76\u63d0\u4f9b\u66f4\u597d\u7684\u6ce8\u91ca"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173038511667827.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 JavaScript \u4e2d\u7f16\u5199\u6e05\u6670\u6709\u6548\u7684\u4ee3\u7801\u6ce8\u91ca\u5e76\u63d0\u4f9b\u66f4\u597d\u7684\u6ce8\u91ca\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 JavaScript \u4e2d\u7f16\u5199\u6e05\u6670\u6709\u6548\u7684\u4ee3\u7801\u6ce8\u91ca\u5e76\u63d0\u4f9b\u66f4\u597d\u7684\u6ce8\u91ca\u63d2\u56fe\" \/><\/p>\n<p>\u5728 javascript \u4e2d\u5de5\u4f5c\u65f6\uff0c\u7f16\u5199\u6e05\u6670\u4e14\u7ed3\u6784\u5316\u7684\u6ce8\u91ca\u5bf9\u4e8e\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002 visual studio code \u7684 <strong>better comments<\/strong> \u6269\u5c55\u901a\u8fc7\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u6ce8\u91ca\u8fdb\u884c\u989c\u8272\u7f16\u7801\u6765\u8fdb\u4e00\u6b65\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u60a8\u53ef\u4ee5\u5728\u8fd9\u91cc\u4e0b\u8f7d\u3002\u8ba9\u6211\u4eec\u63a2\u7d22\u5982\u4f55\u4f7f\u7528\u5b83\u6765\u5b9e\u73b0\u6700\u4f73\u8bc4\u8bba\u5b9e\u8df5\u3002<\/p>\n<h3> \u66f4\u597d\u8bc4\u8bba\u7684\u8bc4\u8bba\u7c7b\u578b <\/h3>\n<p>better comments \u6309\u76ee\u7684\u5bf9\u8bc4\u8bba\u8fdb\u884c\u5206\u7c7b\uff0c\u5305\u62ec\u4ee5\u4e0b\u7c7b\u578b\uff1a<\/p>\n<ol>\n<li> <strong>todos<\/strong> (\/\/ todo:)\uff1a\u6807\u8bb0\u4efb\u52a1\u6216\u6539\u8fdb\u3002<\/li>\n<li> <strong>\u91cd\u8981\u8bf4\u660e<\/strong> (\/\/ !)\uff1a\u7a81\u51fa\u663e\u793a\u4ee3\u7801\u4e2d\u7684\u5173\u952e\u533a\u57df\u3002<\/li>\n<li> <strong>\u95ee\u9898<\/strong> (\/\/ ?)\uff1a\u7528\u4e8e\u6f84\u6e05\u903b\u8f91\u6216\u5bfb\u6c42\u53cd\u9988\u3002<\/li>\n<li> <strong>\u89e3\u91ca<\/strong> (\/\/)\uff1a\u89e3\u91ca\u590d\u6742\u4ee3\u7801\u7684\u6807\u51c6\u6ce8\u91ca\u3002<\/li>\n<\/ol>\n<hr>\n<h3> 1.\u89e3\u91ca\u201c\u4e3a\u4ec0\u4e48\u201d\uff0c\u800c\u4e0d\u662f\u201c\u4ec0\u4e48\u201d <\/h3>\n<p>\u4e0d\u8981\u91cd\u8ff0\u4ee3\u7801\u7684\u4f5c\u7528\uff0c\u800c\u662f\u5173\u6ce8\u4e3a\u4ec0\u4e48\u9700\u8981\u7279\u5b9a\u4ee3\u7801\u3002 <strong>\u66f4\u597d\u7684\u8bc4\u8bba<\/strong> \u5141\u8bb8\u6211\u4eec\u4f7f\u7528 \/\/ ?\u6807\u8bb0\u80fd\u591f\u6f84\u6e05\u63a8\u7406\u7684\u95ee\u9898\u6216\u89e3\u91ca\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>javascript\ncopy code\n\/\/ ? increment by 2 to loop through only odd numbers\nfor (let i = 1; i &lt; 10; i += 2) {\n  console.log(i);\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 2. \u5bf9\u590d\u6742\u903b\u8f91\u4f7f\u7528\u63cf\u8ff0\u6027\u6ce8\u91ca <\/h3>\n<p>\u5bf9\u4e8e\u590d\u6742\u7684\u903b\u8f91\uff0c \/\/!\u7b26\u53f7\u53ef\u4ee5\u6307\u793a<strong>\u66f4\u597d\u7684\u8bc4\u8bba<\/strong>\u4e2d\u7684\u91cd\u8981\u90e8\u5206\u3002\u8fd9\u6709\u52a9\u4e8e\u672a\u6765\u7684\u7ef4\u62a4\u4eba\u5458\u5feb\u901f\u8bc6\u522b\u5fc5\u8981\u7684\u89e3\u91ca\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>javascript\ncopy code\n\/\/! custom sort function to prioritize items with the highest scores, then alphabetically by name\nitems.sort((a, b) =&gt; {\n  if (a.score === b.score) return a.name.localecompare(b.name);\n  return b.score - a.score;\n});\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 3. \u6ce8\u91ca\u51fd\u6570\u548c\u7c7b <\/h3>\n<p>\u5728\u51fd\u6570\u548c\u7c7b\u7684\u5f00\u5934\u63d0\u4f9b\u76ee\u7684\u3001\u8f93\u5165\u548c\u8f93\u51fa\u3002\u4e3a\u4e86\u6e05\u695a\u8d77\u89c1\uff0c\u4f7f\u7528 <strong>\u66f4\u597d\u7684\u8bc4\u8bba<\/strong>\uff1a \/\/ ?\u83b7\u53d6\u89e3\u91ca\u6027\u8bc4\u8bba\u548c \/\/ todo \u5f85\u6539\u8fdb\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>javascript\ncopy code\n\/\/ ? calculates the total price of items in the cart\n\/\/ todo: add handling for discount codes in future iterations\n\/**\n * calculates the total price of items in the cart.\n * @param {array} items - array of item objects with price and quantity.\n * @returns {number} - the total price.\n *\/\nfunction calculatetotal(items) {\n  return items.reduce((total, item) =&gt; total + item.price * item.quantity, 0);\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 4. \u907f\u514d\u591a\u4f59\u7684\u6ce8\u91ca <\/h3>\n<p>\u907f\u514d\u5728\u8bc4\u8bba\u4e2d\u9648\u8ff0\u660e\u663e\u7684\u4fe1\u606f\u3002\u5982\u679c\u51fd\u6570\u540d\u79f0generateid()\u5f88\u6e05\u695a\uff0c\u60a8\u53ef\u4ee5\u5b8c\u5168\u8df3\u8fc7\u6ce8\u91ca\uff0c\u6216\u8005\u4f7f\u7528\u7b80\u5355\u7684 \/\/ ?\u8bc4\u8bba\u4ee5\u6ce8\u660e\u5177\u4f53\u7684\u8bbe\u8ba1\u9009\u62e9\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff08\u907f\u514d\uff09\uff1a<\/strong><\/p>\n<pre>javascript\ncopy code\n\/\/ ? generates a unique identifier string\nfunction generateid() {\n  return math.random().tostring(36).substr(2, 9);\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 5. \u4f7f\u7528\u4e00\u81f4\u7684\u98ce\u683c\u548c\u7ed3\u6784 <\/h3>\n<p>\u9075\u5faa\u4e00\u81f4\u7684\u8bc4\u8bba\u98ce\u683c\uff0c\u5c24\u5176\u662f\u4f7f\u7528<strong>\u66f4\u597d\u7684\u8bc4\u8bba<\/strong>\u989c\u8272\uff0c\u53ef\u4ee5\u5e2e\u52a9\u56e2\u961f\u6210\u5458\u5feb\u901f\u7406\u89e3\u8bc4\u8bba\u5e76\u53d1\u73b0\u91cd\u8981\u6ce8\u91ca\u3002<\/p>\n<h3> 6. \u4fdd\u6301\u8bc4\u8bba\u6700\u65b0 <\/h3>\n<p>\u8fc7\u65f6\u7684\u8bc4\u8bba\u4f1a\u8bef\u5bfc\u8bfb\u8005\u3002\u901a\u8fc7<strong>\u66f4\u597d\u7684\u8bc4\u8bba<\/strong>\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 \/\/ todo \u8fdb\u884c\u63d0\u9192\u6216 \/\/!\u7a81\u51fa\u663e\u793a\u66f4\u6539\u3002<\/p>\n<h3> 7. \u8bb0\u5f55\u5df2\u77e5\u95ee\u9898\u6216\u89e3\u51b3\u65b9\u6cd5 <\/h3>\n<p>\u5982\u679c\u4ee3\u7801\u6709\u89e3\u51b3\u65b9\u6cd5\u6216\u5df2\u77e5\u9650\u5236\uff0c\u8bf7\u4f7f\u7528<strong>\u66f4\u597d\u7684\u6ce8\u91ca<\/strong>\u8bb0\u5f55\u5b83\u4eec\u3002\u8fd9 \/\/ \uff01\u6837\u5f0f\u53ef\u7528\u4e8e\u5173\u952e\u95ee\u9898\uff0c\u63d0\u8bf7\u6ce8\u610f\u4efb\u4f55\u5df2\u77e5\u7684\u9519\u8bef\u6216\u5fc5\u8981\u7684\u4fee\u590d\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>javascript\ncopy code\n\/\/! known issue: this method does not support nested objects; update expected in v2.0\nfunction shallowclone(obj) {\n  return { ...obj };\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 8.\u8bc4\u8bba\u8fb9\u7f18\u6848\u4f8b <\/h3>\n<p>\u4f7f\u7528 \/\/ ?\u5728<strong>\u66f4\u597d\u7684\u8bc4\u8bba<\/strong>\u4e2d\u7a81\u51fa\u663e\u793a\u8fb9\u7f18\u60c5\u51b5\uff0c\u5e2e\u52a9\u672a\u6765\u7684\u8bfb\u8005\u7406\u89e3\u4e3a\u4ec0\u4e48\u5b58\u5728\u67d0\u4e9b\u5904\u7406\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>javascript\nCopy code\n\/\/ ? Handle null values as valid input to indicate 'unknown' data in our system\nfunction processData(input) {\n  if (input === null) return 'unknown';\n  return input;\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u7ed3\u8bba <\/h3>\n<p>\u501f\u52a9 <strong>better comments<\/strong> \u6269\u5c55\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u989c\u8272\u7f16\u7801\u6807\u7b7e\u6765\u9610\u660e\u610f\u56fe\u3001\u6807\u8bb0\u4efb\u52a1\u3001\u7a81\u51fa\u663e\u793a\u91cd\u8981\u90e8\u5206\u4ee5\u53ca\u5904\u7406\u8fb9\u7f18\u60c5\u51b5\uff0c\u4ece\u800c\u4f7f javascript \u6ce8\u91ca\u66f4\u52a0\u6709\u6548\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u6613\u4e8e\u7406\u89e3\u3001\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/p>\n<p>\u5feb\u4e50\u7f16\u7801\u548c\u8bc4\u8bba\u66f4\u597d\u7684\u8bc4\u8bba\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 JavaScript \u4e2d\u7f16\u5199\u6e05\u6670\u6709\u6548\u7684\u4ee3\u7801\u6ce8\u91ca\u5e76\u63d0\u4f9b\u66f4\u597d\u7684\u6ce8\u91ca\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\u5de5\u4f5c\u65f6\uff0c\u7f16\u5199\u6e05\u6670\u4e14\u7ed3\u6784\u5316\u7684\u6ce8\u91ca\u5bf9\u4e8e\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002 visual studio code \u7684 better comments \u6269\u5c55\u901a\u8fc7\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u6ce8\u91ca\u8fdb\u884c\u989c\u8272\u7f16\u7801\u6765\u8fdb\u4e00\u6b65\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u60a8\u53ef\u4ee5\u5728\u8fd9\u91cc\u4e0b\u8f7d\u3002\u8ba9\u6211\u4eec\u63a2\u7d22\u5982\u4f55\u4f7f\u7528\u5b83\u6765\u5b9e\u73b0\u6700\u4f73\u8bc4\u8bba\u5b9e\u8df5\u3002 \u66f4\u597d\u8bc4\u8bba\u7684\u8bc4\u8bba\u7c7b\u578b better comments \u6309\u76ee\u7684\u5bf9\u8bc4\u8bba\u8fdb\u884c\u5206\u7c7b\uff0c\u5305\u62ec\u4ee5\u4e0b\u7c7b\u578b\uff1a todos (\/\/ todo:)\uff1a\u6807\u8bb0\u4efb\u52a1\u6216\u6539\u8fdb\u3002 \u91cd\u8981\u8bf4\u660e (\/\/ !)\uff1a\u7a81\u51fa\u663e\u793a\u4ee3\u7801\u4e2d\u7684\u5173\u952e\u533a\u57df\u3002 \u95ee\u9898 (\/\/ ?)\uff1a\u7528\u4e8e\u6f84\u6e05\u903b\u8f91\u6216\u5bfb\u6c42\u53cd\u9988\u3002 \u89e3\u91ca (\/\/)\uff1a\u89e3\u91ca\u590d\u6742\u4ee3\u7801\u7684\u6807\u51c6\u6ce8\u91ca\u3002 1.\u89e3\u91ca\u201c\u4e3a\u4ec0\u4e48\u201d\uff0c\u800c\u4e0d\u662f\u201c\u4ec0\u4e48\u201d \u4e0d\u8981\u91cd\u8ff0\u4ee3\u7801\u7684\u4f5c\u7528\uff0c\u800c\u662f\u5173\u6ce8\u4e3a\u4ec0\u4e48\u9700\u8981\u7279\u5b9a\u4ee3\u7801\u3002 \u66f4\u597d\u7684\u8bc4\u8bba \u5141\u8bb8\u6211\u4eec\u4f7f\u7528 \/\/ ?\u6807\u8bb0\u80fd\u591f\u6f84\u6e05\u63a8\u7406\u7684\u95ee\u9898\u6216\u89e3\u91ca\u3002 \u793a\u4f8b\uff1a javascript copy code \/\/ ? increment by 2 to loop through only odd numbers for (let i = 1; i &lt; 10; i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1380","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1380"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1380\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}