{"id":10008301,"date":"2023-09-20T20:38:32","date_gmt":"2023-09-20T12:38:32","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008301"},"modified":"2023-09-20T20:38:32","modified_gmt":"2023-09-20T12:38:32","slug":"%e9%99%8d%e4%bd%8e%e4%bb%a3%e7%a0%81%e5%9c%88%e5%a4%8d%e6%9d%82%e5%ba%a6%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008301","title":{"rendered":"\u964d\u4f4e\u4ee3\u7801\u5708\u590d\u6742\u5ea6\u4f18\u5316\u6280\u5de7"},"content":{"rendered":"<p>\u5f53\u4e00\u4e2a\u9879\u76ee\u7ecf\u8fc7\u6301\u7eed\u8fed\u4ee3\uff0c\u4e0d\u65ad\u589e\u52a0\u529f\u80fd\uff0c\u9010\u6e10\u53d8\u6210\u4e00\u4e2a\u590d\u6742\u7684\u4ea7\u54c1\u65f6\uff0c\u65b0\u529f\u80fd\u7684\u5f00\u53d1\u53d8\u5f97\u76f8\u5bf9\u56f0\u96be\u3002\u5176\u4e2d\u4e00\u4e2a\u5f88\u5927\u7684\u539f\u56e0\u662f\u4ee3\u7801\u590d\u6742\u5ea6\u9ad8\uff0c\u5bfc\u81f4\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u90fd\u5f88\u5dee\u3002\u672c\u6587\u5c06\u4ece\u524d\u7aefJavaScript\u7684\u89d2\u5ea6\u51fa\u53d1\uff0c\u4ecb\u7ecd\u4e00\u4e9b\u6709\u6548\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u6765\u4f18\u5316\u524d\u7aef\u4ee3\u7801\u7684<strong>\u5708\u590d\u6742\u5ea6<\/strong>\u3002<\/p>\n<h2>\u4ec0\u4e48\u662f\u5708\u590d\u6742\u5ea6<\/h2>\n<p>\u5708\u590d\u6742\u5ea6\u7684\u8ba1\u7b97\u57fa\u4e8e\u7a0b\u5e8f\u4e2d\u7684\u51b3\u7b56\u7ed3\u6784\uff0c\u5982\u6761\u4ef6\u8bed\u53e5\uff08if\u8bed\u53e5\uff09\u3001\u5faa\u73af\u8bed\u53e5\uff08for\u3001while\u8bed\u53e5\uff09\u3001\u5206\u652f\u8bed\u53e5\uff08switch\u8bed\u53e5\uff09\u7b49\u3002\u6bcf\u5f53\u7a0b\u5e8f\u6d41\u7a0b\u56fe\u4e2d\u589e\u52a0\u4e00\u4e2a\u51b3\u7b56\u70b9\uff0c\u5708\u590d\u6742\u5ea6\u5c31\u4f1a\u589e\u52a01\u3002\u5708\u590d\u6742\u5ea6\u7684\u503c\u8d8a\u9ad8\uff0c\u8868\u793a\u4ee3\u7801\u7684\u590d\u6742\u6027\u8d8a\u5927\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u6d4b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u4e5f\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5708\u590d\u6742\u5ea6\u7684\u63a8\u8350\u503c\u5e94\u8be5\u57281\u523010\u4e4b\u95f4\u3002\u8d85\u8fc710\u7684\u4ee3\u7801\u6a21\u5757\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u91cd\u6784\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7406\u89e3\u6027\u548c\u53ef\u6d4b\u8bd5\u6027\uff0c\u5e76\u964d\u4f4e\u5f15\u5165\u9519\u8bef\u7684\u98ce\u9669\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044234505\" alt=\"\" title=\"\"><\/p>\n<h2>\u8f85\u52a9\u5de5\u5177<\/h2>\n<h3>VScode\u63d2\u4ef6Code Metrics<\/h3>\n<p>VScode\u63d2\u4ef6<code>Code Metrics<\/code>\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u53d1\u73b0\u90a3\u4e9b\u9700\u8981\u4f18\u5316\u590d\u6742\u5ea6\u7684\u4ee3\u7801\uff0c\u5b89\u88c5\u597d\u63d2\u4ef6\u540e\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5728\u4ee3\u7801\u4e0a\u65b9\u4f1a\u51fa\u73b0\u5bf9\u5e94\u7684\u590d\u6742\u5ea6\u503c\uff0c\u6839\u636e\u503c\u7684\u5927\u5c0f\u53ef\u4ee5\u770b\u51fa\u54ea\u4e9b\u4ee3\u7801\u662f\u6025\u9700\u4f18\u5316\u63d0\u5347\u53ef\u8bfb\u6027\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044234506\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u9f20\u6807\u70b9\u51fb\u6240\u63d0\u793a\u590d\u6742\u5ea6\u6570\u503c\u7684\u5730\u65b9\u53ef\u4ee5\u770b\u5230\u5177\u4f53\u662f\u54ea\u4e9b\u4ee3\u7801\u5f71\u54cd\u4e86\u590d\u6742\u5ea6\uff0c\u53ef\u4ee5\u8fdb\u884c\u9488\u5bf9\u6027\u7684\u4f18\u5316\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044234507\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>eslint\u68c0\u67e5<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 eslint \u5e2e\u52a9\u68c0\u67e5\u4ee3\u7801\u7684\u5708\u590d\u6742\u5ea6\uff0c\u5f53\u8d85\u51fa\u4e86\u67d0\u4e2a\u503c\u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n<pre><code class=\"javascript\">rules: {\n  complexity: [\n    'error',\n    {\n      max: 10\n    }\n  ]\n}<\/code><\/pre>\n<p>\u5982\u4e0a\u9762\u7684\u914d\u7f6e\u5c31\u662f\u8d85\u51fa\u4e86 10 \u5c31\u4f1a\u51fa\u73b0\u62a5\u9519\u4fe1\u606f\u3002<\/p>\n<h2>\u5708\u590d\u6742\u5ea6\u7684\u5e38\u7528\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<h3>\u51fd\u6570\u62c6\u5206\u548c\u91cd\u6784\uff0c\u5355\u4e00\u804c\u8d23<\/h3>\n<p>\u8f83\u9ad8\u7684\u5708\u590d\u6742\u5ea6\u5f80\u5f80\u610f\u5473\u7740\u51fd\u6570\u6216\u65b9\u6cd5\u5185\u90e8\u6709\u8fc7\u591a\u7684\u51b3\u7b56\u8def\u5f84\u3002\u901a\u8fc7\u5c06\u590d\u6742\u7684\u51fd\u6570\u5206\u89e3\u6210\u591a\u4e2a\u5c0f\u800c\u6e05\u6670\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u964d\u4f4e\u6bcf\u4e2a\u51fd\u6570\u7684\u5708\u590d\u6742\u5ea6\uff0c\u5e76\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002\u62c6\u5206\u51fd\u6570\u65f6\uff0c\u53ef\u6839\u636e\u529f\u80fd\u6a21\u5757\u6216\u8d23\u4efb\u8fdb\u884c\u5206\u7c7b\uff0c\u786e\u4fdd\u6bcf\u4e2a\u51fd\u6570\u53ea\u8d1f\u8d23\u4e00\u9879\u5177\u4f53\u7684\u4efb\u52a1\u3002<\/p>\n<p>\u4f18\u5316\u524d\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"javascript\">function handle(arr) {\n    \/\/ \u53bb\u91cd\n    let _arr=[],_arrIds=[];\n    for(let i=0;i&lt;arr.length;i++){\n        if(_arrIds.indexOf(arr[i].id)===-1){\n            _arrIds.push(arr[i].id);\n            _arr.push(arr[i]);\n        }\n    }\n    \/\/ \u66ff\u6362\n    _arr.map(item=&gt;{\n        for(let key in item){\n            if(item[key]===''){\n                item[key]='--';\n            }\n        }\n    });\n    \/\/ \u6392\u5e8f\n    _arr.sort((item1,item2)=&gt;item1.id-item2.id);\n    return _arr;\n}<\/code><\/pre>\n<p>\u4f18\u5316\u540e\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"javascript\">function removeDuplicates(arr) {\n  const uniqueArr = [];\n  const uniqueIds = [];\n  \n  for(let i = 0; i &lt; arr.length; i++) {\n    if(uniqueIds.indexOf(arr[i].id) === -1) {\n      uniqueIds.push(arr[i].id);\n      uniqueArr.push(arr[i]);\n    }\n  }\n  \n  return uniqueArr;\n}\n\nfunction replaceEmptyValues(arr) {\n  const processedArr = arr.map(item =&gt; {\n    for(let key in item) {\n      if(item[key] === '') {\n        item[key] = '--';\n      }\n    }\n    return item;\n  });\n  \n  return processedArr;\n}\n\nfunction sortById(arr) {\n  const sortedArr = arr.sort((item1, item2) =&gt; item1.id - item2.id);\n  return sortedArr;\n}\n\nfunction handle(arr) {\n  const uniqueArr = removeDuplicates(arr);\n  const processedArr = replaceEmptyValues(uniqueArr);\n  const sortedArr = sortById(processedArr);\n  return sortedArr;\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5c06\u539f\u59cb\u51fd\u6570\u62c6\u5206\u6210\u4e86\u4e09\u4e2a\u51fd\u6570\u3002<code>removeDuplicates<\/code> \u51fd\u6570\u7528\u4e8e\u53bb\u9664\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c<code>replaceEmptyValues<\/code> \u51fd\u6570\u7528\u4e8e\u904d\u5386\u66ff\u6362\u7a7a\u503c\uff0c<code>sortById<\/code> \u51fd\u6570\u7528\u4e8e\u6839\u636e id \u8fdb\u884c\u6392\u5e8f\u3002\u6bcf\u4e2a\u51fd\u6570\u90fd\u53ea\u8d1f\u8d23\u4e00\u4e2a\u660e\u786e\u7684\u804c\u8d23\u3002<\/p>\n<h3>\u536b\u8bed\u53e5\u53ef\u4ee5\u51cf\u5c11\u5206\u652f<\/h3>\n<p>\u5bf9\u8f93\u5165\u6761\u4ef6\u8fdb\u884c\u591a\u91cd\u5224\u65ad\u65f6\uff0c\u4f7f\u7528\u536b\u8bed\u53e5\u53ef\u4ee5\u51cf\u5c11\u5206\u652f\u8bed\u53e5\u7684\u4f7f\u7528\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<pre><code class=\"javascript\">\/\/ \u4f18\u5316\u524d\nfunction calculateScore(score) {\n  if (score &lt; 0) {\n    return &quot;Invalid score&quot;;\n  } else if (score &lt; 50) {\n    return &quot;Fail&quot;;\n  } else if (score &lt; 70) {\n    return &quot;Pass&quot;;\n  } else if (score &lt; 90) {\n    return &quot;Good&quot;;\n  } else {\n    return &quot;Excellent&quot;;\n  }\n}\n\n\/\/ \u4f18\u5316\u540e\nfunction calculateScore(score) {\n  if (score &lt; 0) {\n    return &quot;Invalid score&quot;;\n  }\n  if (score &lt; 50) {\n    return &quot;Fail&quot;;\n  }\n  if (score &lt; 70) {\n    return &quot;Pass&quot;;\n  }\n  if (score &lt; 90) {\n    return &quot;Good&quot;;\n  }\n  return &quot;Excellent&quot;;\n}<\/code><\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528\u536b\u8bed\u53e5\uff0c\u6211\u4eec\u5c06\u6bcf\u4e2a\u6761\u4ef6\u5224\u65ad\u72ec\u7acb\u51fa\u6765\uff0c\u907f\u514d\u4e86\u5d4c\u5957\u7684\u5206\u652f\u8bed\u53e5\u3002\u8fd9\u79cd\u4f18\u5316\u65b9\u5f0f\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\uff0c\u6bcf\u4e2a\u6761\u4ef6\u5224\u65ad\u90fd\u72ec\u7acb\u6210\u4e3a\u4e00\u4e2a\u903b\u8f91\u5757\uff0c\u5e76\u4e14\u6d88\u9664\u4e86\u4f7f\u7528 else \u7684\u9700\u8981\u3002\u8fd9\u6837\u505a\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u65b9\u4fbf\u4e86\u540e\u7eed\u5bf9\u6bcf\u4e2a\u6761\u4ef6\u5224\u65ad\u7684\u4fee\u6539\u548c\u7ef4\u62a4\u3002<\/p>\n<h3>\u7b80\u5316\u6761\u4ef6\u8868\u8fbe\u5f0f<\/h3>\n<p>\u6709\u76f8\u540c\u903b\u8f91\u4ee3\u7801\u8fdb\u884c\u6761\u4ef6\u5408\u5e76\u8f93\u51fa\uff0c\u51cf\u5c11\u6761\u4ef6\u5224\u65ad\u4ee3\u7801\uff0c\u63d0\u5347\u53ef\u8bfb\u6027\u3002<\/p>\n<pre><code class=\"javascript\">\/\/ \u4f18\u5316\u524d\nfunction a (num) {\n    if (num === 0) {\n        return 0;\n    } else if (num === 1) {\n        return 1;\n    } else if (num === 2) {\n        return 2;\n    } else {\n        return 3;\n    }\n}\n\n\/\/ \u4f18\u5316\u540e\nfunction a (num) {\n    if ([0, 1, 2].indexOf(num) &gt; -1) {\n        return num;\n    } else {\n        return 3;\n    }\n}\n---\n\/\/ \u4f18\u5316\u524d\nfunction a() {\n  if (this.a == 0) return;\n  if (!this.b) return;\n  ...\n}\n\n\/\/ \u4f18\u5316\u540e\nfunction a() {\n  if (this.a == 0 || !this.b) return;\n  ...\n}\n---\n\/\/ \u4f18\u5316\u524d\nfunction a (type) {\n    if (type === 'a') {\n        return 'String';\n    } else if (type === 'b') {\n        return 'Number';\n    } else if (type === 'c') {\n        return 'Object';\n    }\n}\n\n\/\/ \u4f18\u5316\u540e\nfunction a (type) {\n    let obj = {\n        'a': 'String',\n        'b': 'Number',\n        'c': 'Object'\n    };\n    return obj[type];\n}<\/code><\/pre>\n<h3>\u8868\u8fbe\u5f0f\u903b\u8f91\u4f18\u5316<\/h3>\n<p>\u903b\u8f91\u8ba1\u7b97\u4e5f\u4f1a\u589e\u52a0\u5708\u590d\u6742\u5ea6\uff0c\u4f18\u5316\u4e00\u4e9b\u7ed3\u6784\u590d\u6742\u7684\u903b\u8f91\u8868\u8fbe\u5f0f\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u903b\u8f91\u5224\u65ad\uff0c\u4e5f\u5c06\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u964d\u4f4e\u5708\u590d\u6742\u5ea6\u3002<\/p>\n<pre><code class=\"javascript\">\/\/ \u4f18\u5316\u524d\na &amp;&amp; b || a &amp;&amp; c\n\n\/\/ \u4f18\u5316\u540e\na &amp;&amp; (b || c)<\/code><\/pre>\n<h3>\u901a\u8fc7\u591a\u6001\u65b9\u5f0f\u66ff\u4ee3\u6761\u4ef6\u5f0f\u3002<\/h3>\n<p>\u901a\u8fc7\u591a\u6001\u65b9\u5f0f\u66ff\u4ee3\u6761\u4ef6\u5f0f\u662f\u4e00\u79cd\u4f18\u5316\u6280\u5de7\uff0c\u591a\u6001\u5141\u8bb8\u6211\u4eec\u6839\u636e\u4e0d\u540c\u7684\u7c7b\u578b\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u903b\u8f91\u3002<\/p>\n<p>\u4f18\u5316\u524d\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"javascript\">class Shape {\n  constructor(type) {\n    this.type = type;\n  }\n\n  calculateArea() {\n    if (this.type === &quot;circle&quot;) {\n      \/\/ \u8ba1\u7b97\u5706\u5f62\u7684\u9762\u79ef\n    } else if (this.type === &quot;rectangle&quot;) {\n      \/\/ \u8ba1\u7b97\u77e9\u5f62\u7684\u9762\u79ef\n    } else if (this.type === &quot;triangle&quot;) {\n      \/\/ \u8ba1\u7b97\u4e09\u89d2\u5f62\u7684\u9762\u79ef\n    }\n  }\n}<\/code><\/pre>\n<p>\u4f18\u5316\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"javascript\">class Shape {\n  calculateArea() {\n    throw new Error(&quot;calculateArea() method must be implemented&quot;);\n  }\n}\n\nclass Circle extends Shape {\n  calculateArea() {\n    \/\/ \u8ba1\u7b97\u5706\u5f62\u7684\u9762\u79ef\n  }\n}\n\nclass Rectangle extends Shape {\n  calculateArea() {\n    \/\/ \u8ba1\u7b97\u77e9\u5f62\u7684\u9762\u79ef\n  }\n}\n\nclass Triangle extends Shape {\n  calculateArea() {\n    \/\/ \u8ba1\u7b97\u4e09\u89d2\u5f62\u7684\u9762\u79ef\n  }\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u591a\u6001\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u76f8\u5e94\u5bf9\u8c61\u7684<code>calculateArea<\/code>\u65b9\u6cd5\u6765\u6267\u884c\u7279\u5b9a\u5f62\u72b6\u7684\u9762\u79ef\u8ba1\u7b97\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u903b\u8f91\u3002<\/p>\n<h3>\u66ff\u6362\u7b97\u6cd5\uff0c\u4f18\u5316\u590d\u6742\u5ea6<\/h3>\n<p>\u5f53\u53d1\u73b0\u67d0\u4e2a\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u66ff\u6362\u4e3a\u4e00\u4e2a\u5177\u6709\u66f4\u4f18\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u3002<\/p>\n<pre><code class=\"javascript\">\/\/ \u4f18\u5316\u524d\nfunction findDuplicates(nums) {\n  let duplicates = [];\n  for (let i = 0; i &lt; nums.length; i++) {\n    for (let j = i + 1; j &lt; nums.length; j++) {\n      if (nums[i] === nums[j]) {\n        duplicates.push(nums[i]);\n      }\n    }\n  }\n  return duplicates;\n}\n\n\/\/ \u4f18\u5316\u540e\nfunction findDuplicates(nums) {\n  let freq = {};\n  let duplicates = [];\n  for (let num of nums) {\n    if (freq[num]) {\n      duplicates.push(num);\n    } else {\n      freq[num] = true;\n    }\n  }\n  return duplicates;\n}<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f18\u5316\u7b97\u6cd5\u5e76\u4e0d\u603b\u662f\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\u3002\u5728\u9009\u62e9\u66ff\u4ee3\u7b97\u6cd5\u65f6\uff0c\u5e94\u8be5\u7efc\u5408\u8003\u8651\u6570\u636e\u89c4\u6a21\u3001\u7279\u5b9a\u95ee\u9898\u7684\u7279\u6027\u4ee5\u53ca\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u7b49\u56e0\u7d20\u3002<\/p>\n<h3>\u5206\u89e3\u6761\u4ef6\u5f0f\uff0c\u62c6\u5206\u51fd\u6570<\/h3>\n<p>\u5f53\u9047\u5230\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u5f0f\u6216\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5176\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u4f18\u5316\u524d\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"javascript\">function calculateScore(player) {\n  if (player.score &gt;= 100 &amp;&amp; player.level === &quot;expert&quot;) {\n    return player.score * 2;\n  } else if (player.score &gt;= 50 || player.level === &quot;intermediate&quot;) {\n    return player.score * 1.5;\n  } else {\n    return player.score;\n  }\n}<\/code><\/pre>\n<p>\u4f18\u5316\u540e\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"javascript\">function hasHighScore(player) {\n  return player.score &gt;= 100 &amp;&amp; player.level === &quot;expert&quot;;\n}\n\nfunction hasIntermediateScore(player) {\n  return player.score &gt;= 50 || player.level === &quot;intermediate&quot;;\n}\n\nfunction calculateScore(player) {\n  if (hasHighScore(player)) {\n    return player.score * 2;\n  } else if (hasIntermediateScore(player)) {\n    return player.score * 1.5;\n  } else {\n    return player.score;\n  }\n}<\/code><\/pre>\n<p>\u5c06\u539f\u59cb\u7684\u590d\u6742\u6761\u4ef6\u5224\u65ad\u5f0f\u62c6\u5206\u6210\u4e86\u4e24\u4e2a\u72ec\u7acb\u7684\u51fd\u6570\uff1a<code>hasHighScore<\/code>\u548c<code>hasIntermediateScore<\/code>\u3002\u8fd9\u6837<code>calculateScore<\/code>\u51fd\u6570\u4e2d\u7684\u6761\u4ef6\u5224\u65ad\u53d8\u5f97\u66f4\u52a0\u6e05\u6670\u548c\u53ef\u8bfb\u3002\u901a\u8fc7\u5206\u89e3\u6761\u4ef6\u5f0f\u548c\u62c6\u5206\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u91cd\u7528\u6027\u3002<\/p>\n<h3>\u51cf\u5c11<code>return<\/code>\u51fa\u73b0<\/h3>\n<p>\u5f53\u524d\u5927\u591a\u6570\u5708\u590d\u6742\u5ea6\u8ba1\u7b97\u5de5\u5177\u5bf9return\u4e2a\u6570\u4e5f\u8fdb\u884c\u8ba1\u7b97\uff0c\u5982\u679c\u8981\u9488\u5bf9\u8fd9\u4e9b\u5de5\u5177\u8861\u91cf\u89c4\u5219\u8fdb\u884c\u4f18\u5316\uff0c\u51cf\u5c11return\u8bed\u53e5\u4e2a\u6570\u4e5f\u4e3a\u4e00\u79cd\u65b9\u5f0f\u3002<\/p>\n<pre><code class=\"javascript\">\/\/ \u4f18\u5316\u524d\nfunction a(){\n    const value = getSomething();\n    if(value) {\n        return true;\n    } else {\n        return false;\n    }\n}\n\n\/\/ \u4f18\u5316\u540e\nfunction a() {\n    return getSomething();\n}<\/code><\/pre>\n<h3>\u79fb\u9664\u63a7\u5236\u6807\u8bb0\uff0c\u51cf\u5c11\u53d8\u91cf<\/h3>\n<p>\u79fb\u9664\u63a7\u5236\u6807\u8bb0\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u53ef\u8bfb\u6027\u66f4\u9ad8\uff0c\u5e76\u4e14\u51cf\u5c11\u4e86\u4e0d\u5fc5\u8981\u7684\u53d8\u91cf\u4f7f\u7528\u3002<\/p>\n<p>\u4f18\u5316\u524d\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"javascript\">function findFirstPositive(numbers) {\n  let found = false;\n  let firstPositive = null;\n  \n  for (let num of numbers) {\n    if (num &gt; 0) {\n      found = true;\n      firstPositive = num;\n      break;\n    }\n  }\n\n  if (found) {\n    return firstPositive;\n  } else {\n    return -1;\n  }\n}<\/code><\/pre>\n<p>\u4f18\u5316\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"javascript\">function findFirstPositive(numbers) {\n  for (let num of numbers) {\n    if (num &gt; 0) {\n      return num;\n    }\n  }\n\n  return -1;\n}<\/code><\/pre>\n<p>\u5728\u4f18\u5316\u540e\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u5728\u627e\u5230\u7b2c\u4e00\u4e2a\u6b63\u6570\u540e\u7acb\u5373\u8fd4\u56de\u7ed3\u679c\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u63a7\u5236\u6807\u8bb0\u548c\u989d\u5916\u7684\u53d8\u91cf\u3002\u5982\u679c\u904d\u5386\u5b8c\u6574\u4e2a\u6570\u7ec4\u540e\u4ecd\u672a\u627e\u5230\u6b63\u6570\uff0c\u5219\u8fd4\u56de-1\u3002<\/p>\n<h2>\u6700\u540e<\/h2>\n<p>\u5982\u679c\u53ea\u662f\u523b\u677f\u7684\u4f7f\u7528\u5708\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\u53bb\u8861\u91cf\u4e00\u6bb5\u4ee3\u7801\u7684\u6e05\u6670\u5ea6\uff0c\u8fd9\u5e76\u4e0d\u53ef\u53d6\u3002\u5728\u91cd\u6784\u7cfb\u7edf\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7801\u5708\u590d\u6742\u5ea6\u5de5\u5177\u6765\u7edf\u8ba1\u4ee3\u7801\u7684\u590d\u6742\u5ea6\uff0c\u5e76\u5bf9\u590d\u6742\u5ea6\u8f83\u9ad8\u7684\u4ee3\u7801\u8fdb\u884c\u5177\u4f53\u7684\u573a\u666f\u5206\u6790\u3002\u4f46\u4e0d\u662f\u8bf4\u4e00\u5b9a\u8981\u5c06\u590d\u6742\u5ea6\u4f18\u5316\u5230\u67d0\u79cd\u7a0b\u5ea6\uff0c\u5e94\u8be5\u6839\u636e\u5b9e\u9645\u7684\u4e1a\u52a1\u60c5\u51b5\u505a\u51fa\u4f18\u5316\u51b3\u7b56\u3002<\/p>\n<hr>\n<p><strong>\u770b\u5b8c\u672c\u6587\u5982\u679c\u89c9\u5f97\u6709\u7528\uff0c\u8bb0\u5f97\u70b9\u4e2a\u8d5e\u652f\u6301\uff0c\u6536\u85cf\u8d77\u6765\u8bf4\u4e0d\u5b9a\u54ea\u5929\u5c31\u7528\u4e0a\u5566\uff5e<\/strong><\/p>\n<p>\u4e13\u6ce8\u524d\u7aef\u5f00\u53d1\uff0c\u5206\u4eab\u524d\u7aef\u76f8\u5173\u6280\u672f\u5e72\u8d27\uff0c\u516c\u4f17\u53f7\uff1a\u5357\u57ce\u5927\u524d\u7aef\uff08ID: nanchengfe\uff09<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044234503\">https:\/\/segmentfault.com\/a\/1190000044234503<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4e00\u4e2a\u9879\u76ee\u7ecf\u8fc7\u6301\u7eed\u8fed\u4ee3\uff0c\u4e0d\u65ad\u589e\u52a0\u529f\u80fd\uff0c\u9010\u6e10\u53d8\u6210\u4e00\u4e2a\u590d\u6742\u7684\u4ea7\u54c1\u65f6\uff0c\u65b0\u529f\u80fd\u7684\u5f00\u53d1\u53d8\u5f97\u76f8\u5bf9\u56f0\u96be\u3002\u5176\u4e2d\u4e00\u4e2a\u5f88\u5927\u7684\u539f\u56e0\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008301","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008301"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008301\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}