{"id":125,"date":"2022-12-05T17:45:54","date_gmt":"2022-12-05T09:45:54","guid":{"rendered":"https:\/\/www.icfyless.com\/?p=125"},"modified":"2022-12-05T17:45:54","modified_gmt":"2022-12-05T09:45:54","slug":"30-%e4%b8%aa%e5%b8%b8%e7%94%a8-javascript-%e7%9f%a5%e8%af%86%e7%82%b9%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/www.icfyless.com\/index.php\/2022\/12\/05\/30-%e4%b8%aa%e5%b8%b8%e7%94%a8-javascript-%e7%9f%a5%e8%af%86%e7%82%b9%e6%80%bb%e7%bb%93\/","title":{"rendered":"30 \u4e2a\u5e38\u7528 JavaScript \u77e5\u8bc6\u70b9\u603b\u7ed3"},"content":{"rendered":"<h2>30 \u4e2a\u5e38\u7528 JavaScript \u77e5\u8bc6\u70b9\u603b\u7ed3<\/h2>\n<h3>\u4e00\u884c\u4ee3\u7801\u5b8c\u6210\u7ed3\u6784\u52a0\u8d4b\u503c<\/h3>\n<p>\u6211\u4eec\u65e5\u5e38\u7ecf\u5e38\u4f7f\u7528\u7ed3\u6784\u8d4b\u503c\uff0c\u4e00\u822c\u90fd\u662f\u5148\u7ed3\u6784\uff0c\u518d\u8d4b\u503c\uff0c\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e00\u884c\u5c31\u5b8c\u6210\u89e3\u6784\u52a0\u8d4b\u503c\u64cd\u4f5c\uff0c\u770b\u8d77\u6765\u975e\u5e38\u7b80\u5316<\/p>\n<pre><code class=\"language-javascript\">let people = { name: null, age: null };\nlet result = { name: &#039;\u5f20\u4e09&#039;,  age: 16 };\n\n({ name: people.name, age: people.age} = result);\nconsole.log(people) \/\/ {&quot;name&quot;:&quot;\u5f20\u4e09&quot;,&quot;age&quot;:16}###<\/code><\/pre>\n<h3>\u5bf9\u57fa\u7840\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u89e3\u6784<\/h3>\n<p>\u65e5\u5e38\u4e2d\u6211\u4eec\u5e94\u8be5\u7528\u4e0d\u5230\u8fd9\u6837\u7684\u573a\u666f\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u6211\u4eec\u4e5f\u53ef\u4ee5\u5bf9\u57fa\u7840\u6570\u636e\u7c7b\u578b\u89e3\u6784<\/p>\n<pre><code class=\"language-javascript\">const {length : a} = &#039;1234&#039;;\nconsole.log(a) \/\/ 4<\/code><\/pre>\n<h3>\u5bf9\u6570\u7ec4\u89e3\u6784\u5feb\u901f\u62ff\u5230\u6700\u540e\u4e00\u9879\u503c<\/h3>\n<p>\u5b9e\u9645\u4e0a\u6211\u4eec\u662f\u53ef\u4ee5\u5bf9\u6570\u7ec4\u89e3\u6784\u8d4b\u503c\u62ff\u5230<code>length<\/code>\u5c5e\u6027\u7684\uff0c\u901a\u8fc7\u8fd9\u4e2a\u7279\u6027\u4e5f\u53ef\u4ee5\u505a\u66f4\u591a\u7684\u4e8b\u60c5<\/p>\n<pre><code class=\"language-javascript\">const arr = [1, 2, 3];\nconst { 0: first, length, [length - 1]: last } = arr;\nfirst; \/\/ 1\nlast; \/\/ 3\nlength; \/\/ 3<\/code><\/pre>\n<h3>\u5c06\u4e0b\u6807\u8f6c\u4e3a\u4e2d\u6587\u96f6\u4e00\u4e8c\u4e09...<\/h3>\n<p>\u65e5\u5e38\u53ef\u80fd\u6709\u7684\u5217\u8868\u6211\u4eec\u9700\u8981\u5c06\u5bf9\u5e94\u7684012345\u8f6c\u4e3a\u4e2d\u6587\u7684\u4e00\u3001\u4e8c\u3001\u4e09\u3001\u56db\u3001\u4e94...\uff0c\u5728\u8001\u7684\u9879\u76ee\u770b\u5230\u8fd8\u6709\u901a\u8fc7\u81ea\u5df1\u624b\u52a8\u5b9a\u4e49\u5f88\u591a\u884c\u8fd9\u6837\u7684\u5199\u6cd5\uff0c\u4e8e\u662f\u5199\u4e86\u4e00\u4e2a\u8fd9\u6837\u7684\u65b9\u6cd5\u8f6c\u6362<\/p>\n<pre><code class=\"language-javascript\">export function transfromNumber(number){\n  const  INDEX_MAP = [&#039;\u96f6&#039;\uff0c&#039;\u4e00&#039;.....]\n  if(!number) return\n  if(number === 10) return INDEX_MAP[number]\n  return [...number.toString()].reduce( (pre, cur) =&gt; pre  + INDEX_MAP[cur] , &#039;&#039; )\n}<\/code><\/pre>\n<h3>\u5224\u65ad\u6574\u6570\u7684\u4e0d\u540c\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-javascript\">\/* 1.\u4efb\u4f55\u6574\u6570\u90fd\u4f1a\u88ab1\u6574\u9664\uff0c\u5373\u4f59\u6570\u662f0\u3002\u5229\u7528\u8fd9\u4e2a\u89c4\u5219\u6765\u5224\u65ad\u662f\u5426\u662f\u6574\u6570\u3002\u4f46\u662f\u5bf9\u5b57\u7b26\u4e32\u4e0d\u51c6\u786e *\/\nfunction isInteger(obj) {\n return obj%1 === 0\n}\n\n\/* 1. \u6dfb\u52a0\u4e00\u4e2a\u662f\u6570\u5b57\u7684\u5224\u65ad *\/\nfunction isInteger(obj) {\n return typeof obj === &#039;number&#039; &amp;&amp; obj%1 === 0\n}\n\n\/* 2. \u4f7f\u7528Math.round\u3001Math.ceil\u3001Math.floor\u5224\u65ad \u6574\u6570\u53d6\u6574\u540e\u8fd8\u662f\u7b49\u4e8e\u81ea\u5df1\u3002\u5229\u7528\u8fd9\u4e2a\u7279\u6027\u6765\u5224\u65ad\u662f\u5426\u662f\u6574\u6570*\/\nfunction isInteger(obj) {\n return Math.floor(obj) === obj\n}\n\n\/* 3. \u901a\u8fc7parseInt\u5224\u65ad \u67d0\u4e9b\u573a\u666f\u4e0d\u51c6\u786e *\/\nfunction isInteger(obj) {\n return parseInt(obj, 10) === obj\n}\n\n\/* 4. \u901a\u8fc7\u4f4d\u8fd0\u7b97\u7b26*\/\nfunction isInteger(obj) {\n return (obj | 0) === obj\n}<\/code><\/pre>\n<h3>\u901a\u8fc7css\u68c0\u6d4b\u7cfb\u7edf\u7684\u4e3b\u9898\u8272\u4ece\u800c\u5168\u5c40\u4fee\u6539\u6837\u5f0f<\/h3>\n<p><code>@media<\/code>\u7684\u5c5e\u6027<code>prefers-color-scheme<\/code>\u5c31\u53ef\u4ee5\u77e5\u9053\u5f53\u524d\u7684\u7cfb\u7edf\u4e3b\u9898\uff0c\u5f53\u7136\u4f7f\u7528\u524d\u9700\u8981\u67e5\u67e5\u517c\u5bb9\u6027<\/p>\n<pre><code class=\"language-css\">@media (prefers-color-scheme: dark) { \/\/... } \n@media (prefers-color-scheme: light) { \/\/... }<\/code><\/pre>\n<p>javascript\u4e5f\u53ef\u4ee5\u8f7b\u677e\u505a\u5230<\/p>\n<pre><code class=\"language-javascript\">window.addEventListener(&#039;theme-mode&#039;, event =&gt;{ \n    if(event.mode == &#039;dark&#039;){}\n   if(event.mode == &#039;light&#039;){} \n})\n\nwindow.matchMedia(&#039;(prefers-color-scheme: dark)&#039;) .addEventListener(&#039;change&#039;, event =&gt; { \n    if (event.matches) {} \/\/ dark mode\n})<\/code><\/pre>\n<h3>\u6570\u7ec4\u968f\u673a\u6253\u4e71\u987a\u5e8f<\/h3>\n<p>\u901a\u8fc7<code>0.5<\/code>-<code>Math.random()<\/code>\u5f97\u5230\u4e00\u4e2a\u968f\u673a\u6570\uff0c\u518d\u901a\u8fc7\u4e24\u6b21<code>sort<\/code>\u6392\u5e8f\u6253\u4e71\u7684\u66f4\u5f7b\u5e95,\u4f46\u662f\u8fd9\u4e2a\u65b9\u6cd5\u5b9e\u9645\u4e0a\u5e76\u4e0d\u591f\u968f\u673a\uff0c\u5982\u679c\u662f\u4f01\u4e1a\u7ea7\u8fd0\u7528\uff0c\u5efa\u8bae\u4f7f\u7528\u7b2c\u4e8c\u79cd\u6d17\u724c\u7b97\u6cd5<\/p>\n<pre><code class=\"language-javascript\">shuffle(arr) {\n      return arr.sort(() =&gt; 0.5 - Math.random()). sort(() =&gt; 0.5 - Math.random());\n },<\/code><\/pre>\n<pre><code class=\"language-javascript\">function shuffle(arr) {\n  for (let i = arr.length - 1; i &gt; 0; i--) {\n    const randomIndex = Math.floor(Math.random() * (i + 1))\n    ;[arr[i], arr[randomIndex]] = [arr[randomIndex], arr[i]]\n  }\n  return arr\n}<\/code><\/pre>\n<h3>\u968f\u673a\u83b7\u53d6\u4e00\u4e2aBoolean\u503c<\/h3>\n<p>\u548c\u4e0a\u4e2a\u539f\u7406\u76f8\u540c\uff0c\u901a\u8fc7\u968f\u673a\u6570\u83b7\u53d6\uff0c<code>Math.random()<\/code>\u7684\u533a\u95f4\u662f<code>0-0.99<\/code>\uff0c\u7528<code>0.5<\/code>\u5728\u4e2d\u95f4\u767e\u5206\u4e4b\u4e94\u5341\u7684\u6982\u7387<\/p>\n<pre><code class=\"language-javascript\">function randomBool() {\n    return 0.5 - Math.random()\n}<\/code><\/pre>\n<h3>\u628a\u6570\u7ec4\u6700\u540e\u4e00\u9879\u79fb\u5230\u7b2c\u4e00\u9879<\/h3>\n<pre><code class=\"language-javascript\">function (arr){\n    return arr.push(arr.shift());\n}<\/code><\/pre>\n<h3>\u628a\u6570\u7ec4\u7684\u7b2c\u4e00\u9879\u653e\u5230\u6700\u540e\u4e00\u9879<\/h3>\n<pre><code class=\"language-javascript\">function(arr){\n  return arr.unshift(arr.pop());\n}<\/code><\/pre>\n<h3>\u5229\u7528set\u6570\u7ec4\u53bb\u91cd<\/h3>\n<pre><code class=\"language-javascript\">function uniqueArr(arr){\n    return [...new Set(arr)]\n}<\/code><\/pre>\n<h3>dom\u8282\u70b9\u5e73\u6ed1\u6eda\u52a8\u5230\u53ef\u662f\u533a\u57df\uff0c\u9876\u90e8\uff0c\u5e95\u90e8<\/h3>\n<p>\u539f\u751f\u7684<code>scrollTo<\/code>\u65b9\u6cd5\u6ca1\u6709\u52a8\u753b\uff0c\u7c7b\u4f3c\u4e8e\u951a\u70b9\u8df3\u8f6c\uff0c\u6bd4\u8f83\u751f\u786c\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u81ea\u5e26\u5e73\u6ed1\u7684\u8fc7\u5ea6\u6548\u679c<\/p>\n<pre><code class=\"language-javascript\">function scrollTo(element) {\n    element.scrollIntoView({ behavior: &quot;smooth&quot;, block: &quot;start&quot; }) \/\/ \u9876\u90e8\n    element.scrollIntoView({ behavior: &quot;smooth&quot;, block: &quot;end&quot; }) \/\/ \u5e95\u90e8\n    element.scrollIntoView({ behavior: &quot;smooth&quot;}) \/\/ \u53ef\u89c6\u533a\u57df\n}<\/code><\/pre>\n<h3>\u83b7\u53d6\u968f\u673a\u989c\u8272<\/h3>\n<pre><code class=\"language-javascript\">function getRandomColor(){\n    return `#${Math.floor(Math.random() * 0xffffff) .toString(16)}`;\n}<\/code><\/pre>\n<h3>\u68c0\u6d4b\u662f\u5426\u4e3a\u7a7a\u5bf9\u8c61<\/h3>\n<p>\u901a\u8fc7\u4f7f\u7528Es6\u7684<code>Reflect<\/code>\u9759\u6001\u65b9\u6cd5\u5224\u65ad\u4ed6\u7684\u957f\u5ea6\u5c31\u53ef\u4ee5\u5224\u65ad\u662f\u5426\u662f\u7a7a\u6570\u7ec4\u4e86\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>Object.keys()<\/code>\u6765\u5224\u65ad<\/p>\n<pre><code class=\"language-javascript\">function isEmpty(obj){\n    return  Reflect.ownKeys(obj).length === 0 &amp;&amp; obj.constructor === Object;\n}<\/code><\/pre>\n<h3>Boolean\u8f6c\u6362<\/h3>\n<pre><code class=\"language-javascript\">function toBoolean(value, truthyValues = [&#039;true&#039;]){\n  const normalizedValue = String(value).toLowerCase().trim();\n  return truthyValues.includes(normalizedValue);\n}\ntoBoolean(&#039;TRUE&#039;); \/\/ true\ntoBoolean(&#039;FALSE&#039;); \/\/ false\ntoBoolean(&#039;YES&#039;, [&#039;yes&#039;]); \/\/ true<\/code><\/pre>\n<h3>\u5404\u79cd\u6570\u7ec4\u514b\u9686\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-javascript\">const clone = (arr) =&gt; arr.slice(0);\nconst clone = (arr) =&gt; [...arr];\nconst clone = (arr) =&gt; Array.from(arr);\nconst clone = (arr) =&gt; arr.map((x) =&gt; x);\nconst clone = (arr) =&gt; JSON.parse(JSON.stringify(arr));\nconst clone = (arr) =&gt; arr.concat([]);\nconst clone = (arr) =&gt; structuredClone(arr);<\/code><\/pre>\n<h3>\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u5927\u5c0f<\/h3>\n<p>\u901a\u8fc7\u8c03\u7528<code>getTime<\/code>\u83b7\u53d6\u65f6\u95f4\u6233\u6bd4\u8f83\u5c31\u53ef\u4ee5\u4e86<\/p>\n<pre><code class=\"language-javascript\">function compare(a, b){\n    return a.getTime() &gt; b.getTime();\n}<\/code><\/pre>\n<h3>\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u4e4b\u95f4\u7684\u6708\u4efd\u5dee\u5f02<\/h3>\n<pre><code class=\"language-javascript\">function monthDiff(startDate, endDate){\n    return  Math.max(0, (endDate.getFullYear() - startDate.getFullYear()) * 12 - startDate.getMonth() + endDate.getMonth());\n}<\/code><\/pre>\n<h3>\u4e00\u6b65\u4ece\u65f6\u95f4\u4e2d\u63d0\u53d6\u5e74\u6708\u65e5\u65f6\u5206\u79d2<\/h3>\n<p>\u65f6\u95f4\u683c\u5f0f\u5316\u8f7b\u677e\u89e3\u51b3\uff0c\u4e00\u6b65\u83b7\u53d6\u5230\u5e74\u6708\u65e5\u65f6\u5206\u79d2\u6beb\u79d2\uff0c\u7531\u4e8e<code>toISOString<\/code>\u4f1a\u4e22\u5931\u65f6\u533a\uff0c\u5bfc\u81f4\u65f6\u95f4\u5dee\u516b\u5c0f\u65f6\uff0c\u6240\u4ee5\u5728\u683c\u5f0f\u5316\u4e4b\u524d\u6211\u4eec\u52a0\u4e0a\u516b\u4e2a\u5c0f\u65f6\u65f6\u95f4\u5373\u53ef<\/p>\n<pre><code class=\"language-javascript\">function extract(date){\n   const d = new Date(new Date(date).getTime() + 8*3600*1000);\n  return new Date(d).toISOString().split(\/[^0-9]\/).slice(0, -1);\n}\nconsole.log(extract(new Date())) \/\/ [&#039;2022&#039;, &#039;09&#039;, &#039;19&#039;, &#039;18&#039;, &#039;06&#039;, &#039;11&#039;, &#039;187&#039;]<\/code><\/pre>\n<h3>\u5224\u65ad\u4e00\u4e2a\u53c2\u6570\u662f\u4e0d\u662f\u51fd\u6570<\/h3>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u7684\u65b9\u6cd5\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u51fd\u6570\u56de\u8c03\uff0c\u4f46\u662f\u9700\u8981\u68c0\u6d4b\u5176\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>Object<\/code>\u7684\u539f\u578b\u65b9\u6cd5\u53bb\u68c0\u6d4b\uff0c\u5f53\u7136\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u51c6\u786e\u68c0\u6d4b\u4efb\u4f55\u7c7b\u578b\u3002<\/p>\n<pre><code class=\"language-javascript\">function isFunction(v){\n   return [&#039;[object Function]&#039;, &#039;[object GeneratorFunction]&#039;, &#039;[object AsyncFunction]&#039;, &#039;[object Promise]&#039;].includes(Object.prototype.toString.call(v));\n}<\/code><\/pre>\n<h3>\u8ba1\u7b97\u4e24\u4e2a\u5750\u6807\u4e4b\u95f4\u7684\u8ddd\u79bb<\/h3>\n<pre><code class=\"language-javascript\">function distance(p1, p2){\n    return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));\n}<\/code><\/pre>\n<h3>\u68c0\u6d4b\u4e24\u4e2adom\u8282\u70b9\u662f\u5426\u8986\u76d6\u91cd\u53e0<\/h3>\n<p>\u6709\u4e9b\u573a\u666f\u4e0b\u6211\u4eec\u9700\u8981\u5224\u65addom\u662f\u5426\u53d1\u751f\u78b0\u649e\u4e86\u6216\u8005\u91cd\u53e0\u4e86\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>getBoundingClientRect<\/code>\u83b7\u53d6\u5230dom\u7684<code>x1<\/code>,<code>y1<\/code>,<code>x2<\/code>,<code>y2<\/code>\u5750\u6807\u7136\u540e\u8fdb\u884c\u5750\u6807\u6bd4\u5bf9\u5373\u53ef\u5224\u65ad\u51fa\u6765<\/p>\n<pre><code class=\"language-javascript\">function overlaps = (a, b) {\n   return (a.x1 &lt; b.x2 &amp;&amp; b.x1 &lt; a.x2) || (a.y1 &lt; b.y2 &amp;&amp; b.y1 &lt; a.y2);\n}<\/code><\/pre>\n<h3>\u5224\u65ad\u662f\u5426\u662fNodeJs\u73af\u5883<\/h3>\n<p>\u524d\u7aef\u7684\u65e5\u5e38\u5f00\u53d1\u662f\u79bb\u4e0d\u5f00nodeJs\u7684\uff0c\u901a\u8fc7\u5224\u65ad\u5168\u5c40\u73af\u5883\u6765\u68c0\u6d4b\u662f\u5426\u662fnodeJs\u73af\u5883<\/p>\n<pre><code class=\"language-javascript\">function isNode(){\n    return typeof process !== &#039;undefined&#039; &amp;&amp; process.versions != null &amp;&amp; process.versions.node != null;\n}<\/code><\/pre>\n<h3>\u53c2\u6570\u6c42\u548c<\/h3>\n<p>\u4e4b\u524d\u770b\u5230\u6709\u901a\u8fc7\u51fd\u6570\u67ef\u7406\u5316\u5f62\u5f0f\u6765\u6c42\u548c\u7684\uff0c\u901a\u8fc7<code>reduce<\/code>\u4e00\u884c\u5373\u53ef<\/p>\n<pre><code class=\"language-javascript\">function sum(...args){\n    args.reduce((a, b) =&gt; a + b);\n}<\/code><\/pre>\n<blockquote>\n<p><a href=\"https:\/\/juejin.cn\/post\/7145036326373425159\">\u672c\u6587\u5f15\u7528\u81ea:30\u4e2aJavascript\u77e5\u8bc6\u70b9\u603b\u7ed3\uff0c\u603b\u6709\u4f60\u4e0d\u4f1a\u7684\uff01<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>30 \u4e2a\u5e38\u7528 JavaScript \u77e5\u8bc6\u70b9\u603b\u7ed3 \u4e00 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":124,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":2,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions\/127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/media\/124"}],"wp:attachment":[{"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}