{"id":3117,"date":"2020-11-05T08:34:46","date_gmt":"2020-11-05T00:34:46","guid":{"rendered":"http:\/\/www.yuguoxy.com\/geekhome\/?p=3117"},"modified":"2026-08-18T12:35:33","modified_gmt":"2026-08-18T04:35:33","slug":"24%e4%b8%aa%e8%a7%a3%e5%86%b3%e5%ae%9e%e9%99%85%e9%97%ae%e9%a2%98%e7%9a%84es6%e4%bb%a3%e7%a0%81%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=3117","title":{"rendered":"24\u4e2a\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u7684ES6\u4ee3\u7801\u6bb5"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u8fd9\u7bc7\u6587\u7ae0\u57fa\u4e8e\u5b9e\u9645\u4f7f\u7528\u573a\u666f\u603b\u7ed3\u4e86 24 \u4e2a ES6 \u4ee3\u7801\u6bb5\uff0c\u53ef\u7528\u6765\u89e3\u51b3\u9879\u76ee\u91cc\u4e00\u6279\u5e38\u89c1\u95ee\u9898\uff1a\u663e\u9690 DOM\u3001\u6eda\u52a8\u3001URL \u53c2\u6570\u3001\u8868\u5355\u5e8f\u5217\u5316\u3001XHR\u3001\u526a\u8d34\u677f\u548c Node \u5efa\u76ee\u5f55\u3002\u4f5c\u8005 Madza\uff0c\u8bd1\u8005\u738b\u5f3a\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u82f1\u6587\u539f\u6587\uff1a<a href=\"https:\/\/madza.hashnode.dev\/24-modern-es6-code-snippets-to-solve-practical-js-problems\" target=\"_blank\" class=\"url\" rel=\"noopener\">https:\/\/madza.hashnode.dev\/24-modern-es6-code-snippets-to-solve-practical-js-problems<\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u6309\u300cDOM \u4e0e\u9875\u9762 \u2192 URL \u4e0e\u4e8b\u4ef6 \u2192 \u65f6\u95f4\u4e0e\u7f51\u7edc \u2192 \u5de5\u5177\u300d\u5206\u7ec4\uff0c24 \u6bb5\u90fd\u4fdd\u7559\uff0c\u53ea\u628a\u6324\u5728\u4e00\u884c\u7684\u793a\u4f8b\u62c6\u5f00\uff0c\u5e76\u628a\u53d6\u56fe\u7247\u5c5e\u6027\u4ece\u539f\u6587\u7684 hide \u6539\u56de src\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5206\u7ec4<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u7f16\u53f7<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5178\u578b\u95ee\u9898<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">DOM \u4e0e\u9875\u9762<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">1\u20139<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u663e\u9690\u3001class\u3001\u6eda\u52a8\u3001\u89c6\u53e3\u3001\u56fe\u7247<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">URL \u4e0e\u4e8b\u4ef6<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">10\u201316<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5f53\u524d URL\u3001query\u3001\u8868\u5355\u3001\u81ea\u5b9a\u4e49\u4e8b\u4ef6<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u65f6\u95f4\u4e0e\u7f51\u7edc<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">17\u201321<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u65f6\u957f\u683c\u5f0f\u5316\u3001\u65e5\u671f\u5dee\u3001GET\/POST\u3001\u8ba1\u6570\u5668<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5de5\u5177<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">22\u201324<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u526a\u8d34\u677f\u3001\u6807\u7b7e\u9875\u7126\u70b9\u3001\u521b\u5efa\u76ee\u5f55<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001DOM \u4e0e\u9875\u9762<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1\u3001\u5982\u4f55\u9690\u85cf\u6240\u6709\u6307\u5b9a\u5143\u7d20\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const hide = (...el) => [...el].forEach(e => (e.style.display = 'none'));\n\/\/ Example\nhide(document.querySelectorAll('img')); \/\/ \u9690\u85cf\u9875\u9762\u4e0a\u7684\u6240\u6709 img \u5143\u7d20<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2\u3001\u5982\u4f55\u786e\u8ba4\u5143\u7d20\u662f\u5426\u5177\u6709\u6307\u5b9a\u7684\u7c7b\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const hasClass = (el, className) => el.classList.contains(className);\n\/\/ Example\nhasClass(document.querySelector('p.special'), 'special'); \/\/ true<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">3\u3001\u5982\u4f55\u5207\u6362\u5143\u7d20\u7684\u7c7b\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const toggleClass = (el, className) => el.classList.toggle(className);\n\/\/ Example\n\/\/ toggleClass(document.querySelector('p.special'), 'special');\n\/\/ \u8be5\u6bb5\u4e0d\u518d\u6709 'special' \u7c7b<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">4\u3001\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684\u6eda\u52a8\u4f4d\u7f6e\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const getScrollPosition = (el = window) => ({\n  x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,\n  y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop\n});\n\/\/ Example\ngetScrollPosition(); \/\/ {x: 0, y: 200}<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">5\u3001\u5982\u4f55\u5e73\u6ed1\u6eda\u52a8\u5230\u9875\u9762\u9876\u90e8\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const scrollToTop = () => {\n  const c = document.documentElement.scrollTop || document.body.scrollTop;\n  if (c > 0) {\n    window.requestAnimationFrame(scrollToTop);\n    window.scrollTo(0, c - c \/ 8);\n  }\n};\n\/\/ Example\nscrollToTop();<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">6\u3001\u5982\u4f55\u786e\u8ba4\u7236\u5143\u7d20\u662f\u5426\u5305\u542b\u5b50\u5143\u7d20\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const elementContains = (parent, child) => parent !== child &amp;&amp; parent.contains(child);\n\/\/ Examples\nelementContains(document.querySelector('head'), document.querySelector('title')); \/\/ true\nelementContains(document.querySelector('body'), document.querySelector('body')); \/\/ false<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">7\u3001\u5982\u4f55\u786e\u8ba4\u6307\u5b9a\u5143\u7d20\u662f\u5426\u5728\u89c6\u53e3\u53ef\u89c1\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const elementIsVisibleInViewport = (el, partiallyVisible = false) => {\n  const { top, left, bottom, right } = el.getBoundingClientRect();\n  const { innerHeight, innerWidth } = window;\n  return partiallyVisible\n    ? ((top > 0 &amp;&amp; top &lt; innerHeight) || (bottom > 0 &amp;&amp; bottom &lt; innerHeight))\n      &amp;&amp; ((left > 0 &amp;&amp; left &lt; innerWidth) || (right > 0 &amp;&amp; right &lt; innerWidth))\n    : top >= 0 &amp;&amp; left >= 0 &amp;&amp; bottom &lt;= innerHeight &amp;&amp; right &lt;= innerWidth;\n};\n\/\/ Examples\nelementIsVisibleInViewport(el); \/\/ (\u4e0d\u5b8c\u5168\u53ef\u89c1)\nelementIsVisibleInViewport(el, true); \/\/ (\u90e8\u5206\u53ef\u89c1)<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">8\u3001\u5982\u4f55\u83b7\u53d6\u4e00\u4e2a\u5143\u7d20\u5185\u7684\u6240\u6709\u56fe\u50cf\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const getImages = (el, includeDuplicates = false) => {\n  const images = [...el.getElementsByTagName('img')].map(img => img.getAttribute('src'));\n  return includeDuplicates ? images : [...new Set(images)];\n};\n\/\/ Examples\ngetImages(document, true); \/\/ ['image1.jpg', 'image2.png', 'image1.png', '...']\ngetImages(document, false); \/\/ ['image1.jpg', 'image2.png', '...']<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">9\u3001\u5982\u4f55\u5206\u8fa8\u8bbe\u5907\u662f\u79fb\u52a8\u8bbe\u5907\u8fd8\u662f\u684c\u9762\u8bbe\u5907\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const detectDeviceType = () =>\n  \/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini\/i.test(navigator.userAgent)\n    ? 'Mobile'\n    : 'Desktop';\n\/\/ Example\ndetectDeviceType(); \/\/ \"Mobile\" or \"Desktop\" <\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001URL\u3001\u8868\u5355\u4e0e\u4e8b\u4ef6<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">10\u3001\u5982\u4f55\u83b7\u53d6\u5f53\u524d URL\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const currentURL = () => window.location.href;\n\/\/ Example\ncurrentURL(); \/\/ 'https:\/\/google.com'<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">11\u3001\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5f53\u524d URL \u53c2\u6570\u7684\u5bf9\u8c61\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const getURLParameters = url =>\n  (url.match(\/([^?=&amp;]+)(=([^&amp;]*))\/g) || []).reduce(\n    (a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),\n    {}\n  );\n\/\/ Examples\ngetURLParameters('http:\/\/url.com\/page?n=Adam&amp;s=Smith'); \/\/ {n: 'Adam', s: 'Smith'}\ngetURLParameters('google.com'); \/\/ {}<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">12\u3001\u5982\u4f55\u5c06\u4e00\u7ec4\u8868\u5355\u5143\u7d20\u7f16\u7801\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const formToObject = form =>\n  Array.from(new FormData(form)).reduce(\n    (acc, [key, value]) => ({\n      ...acc,\n      [key]: value\n    }),\n    {}\n  );\n\/\/ Example\nformToObject(document.querySelector('#form')); \/\/ { email: 'test@email.com', name: 'Test Name' }<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">13\u3001\u5982\u4f55\u4ece\u5bf9\u8c61\u4e2d\u68c0\u7d22\u7ed9\u5b9a\u9009\u62e9\u5668\u6307\u793a\u7684\u4e00\u7ec4\u5c5e\u6027\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const get = (from, ...selectors) =>\n  [...selectors].map(s =>\n    s\n      .replace(\/\\[([^\\[\\]]*)\\]\/g, '.$1.')\n      .split('.')\n      .filter(t => t !== '')\n      .reduce((prev, cur) => prev &amp;&amp; prev[cur], from)\n  );\nconst obj = { selector: { to: { val: 'val to select' } }, target: [1, 2, { a: 'test' }] };\n\/\/ Example\nget(obj, 'selector.to.val', 'target[0]', 'target[2].a'); \/\/ ['val to select', 1, 'test']<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">14\u3001\u5982\u4f55\u5728\u7b49\u5f85\u4e00\u5b9a\u65f6\u95f4\u540e\u8c03\u7528\u63d0\u4f9b\u7684\u51fd\u6570\uff08\u5355\u4f4d\u6beb\u79d2\uff09\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);\ndelay(\n  function(text) {\n    console.log(text);\n  },\n  1000,\n  'later'\n);\n\/\/ \u4e00\u79d2\u540e\u8bb0\u5f55 'later' \u3002<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">15\u3001\u5982\u4f55\u5728\u7ed9\u5b9a\u5143\u7d20\u4e0a\u89e6\u53d1\u7279\u5b9a\u4e8b\u4ef6\uff0c\u4e14\u53ef\u9009\u4f20\u9012\u81ea\u5b9a\u4e49\u6570\u636e\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const triggerEvent = (el, eventType, detail) =>\n  el.dispatchEvent(new CustomEvent(eventType, { detail }));\n\/\/ Examples\ntriggerEvent(document.getElementById('myId'), 'click');\ntriggerEvent(document.getElementById('myId'), 'click', { username: 'bob' });<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">16\u3001\u5982\u4f55\u79fb\u9664\u4e00\u4e2a\u5143\u7d20\u7684\u4e8b\u4ef6\u4fa6\u542c\u5668\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const off = (el, evt, fn, opts = false) => el.removeEventListener(evt, fn, opts);\nconst fn = () => console.log('!');\ndocument.body.addEventListener('click', fn);\noff(document.body, 'click', fn);\n\/\/ no longer logs '!' upon clicking on the page<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u65f6\u95f4\u4e0e\u7f51\u7edc<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">17\u3001\u5982\u4f55\u83b7\u5f97\u7ed9\u5b9a\u6beb\u79d2\u6570\u7684\u53ef\u8bfb\u683c\u5f0f\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const formatDuration = ms => {\n  if (ms &lt; 0) ms = -ms;\n  const time = {\n    day: Math.floor(ms \/ 86400000),\n    hour: Math.floor(ms \/ 3600000) % 24,\n    minute: Math.floor(ms \/ 60000) % 60,\n    second: Math.floor(ms \/ 1000) % 60,\n    millisecond: Math.floor(ms) % 1000\n  };\n  return Object.entries(time)\n    .filter(val => val[1] !== 0)\n    .map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`)\n    .join(', ');\n};\n\/\/ Examples\nformatDuration(1001); \/\/ '1 second, 1 millisecond'\nformatDuration(34325055574); \/\/ '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">18\u3001\u5982\u4f55\u83b7\u53d6\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u95f4\u9694\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const getDaysDiffBetweenDates = (dateInitial, dateFinal) =>\n  (dateFinal - dateInitial) \/ (1000 * 3600 * 24);\n\/\/ Example\ngetDaysDiffBetweenDates(new Date('2017-12-13'), new Date('2017-12-22')); \/\/ 9<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">19\u3001\u5982\u4f55\u5bf9\u4f20\u9012\u7684 URL \u8fdb\u884c GET \u8bf7\u6c42\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const httpGet = (url, callback, err = console.error) => {\n  const request = new XMLHttpRequest();\n  request.open('GET', url, true);\n  request.onload = () => callback(request.responseText);\n  request.onerror = () => err(request);\n  request.send();\n};\nhttpGet(\n  'https:\/\/jsonplaceholder.typicode.com\/posts\/1',\n  console.log\n);\n\/\/ Logs: {\"userId\": 1, \"id\": 1, \"title\": \"sample title\", \"body\": \"my text\"}<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">20\u3001\u5982\u4f55\u5bf9\u4f20\u9012\u7684 URL \u8fdb\u884c POST \u8bf7\u6c42\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const httpPost = (url, data, callback, err = console.error) => {\n  const request = new XMLHttpRequest();\n  request.open('POST', url, true);\n  request.setRequestHeader('Content-type', 'application\/json; charset=utf-8');\n  request.onload = () => callback(request.responseText);\n  request.onerror = () => err(request);\n  request.send(data);\n};\nconst newPost = {\n  userId: 1,\n  id: 1337,\n  title: 'Foo',\n  body: 'bar bar bar'\n};\nconst data = JSON.stringify(newPost);\nhttpPost(\n  'https:\/\/jsonplaceholder.typicode.com\/posts',\n  data,\n  console.log\n);\n\/\/ Logs: {\"userId\": 1, \"id\": 1337, \"title\": \"Foo\", \"body\": \"bar bar bar\"}<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">21\u3001\u5982\u4f55\u4e3a\u6307\u5b9a\u9009\u62e9\u5668\u521b\u5efa\u5177\u6709\u6307\u5b9a\u8303\u56f4\u3001\u6b65\u957f\u548c\u6301\u7eed\u65f6\u95f4\u7684\u8ba1\u65f6\u5668\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const counter = (selector, start, end, step = 1, duration = 2000) => {\n  let current = start,\n    _step = (end - start) * step &lt; 0 ? -step : step,\n    timer = setInterval(() => {\n      current += _step;\n      document.querySelector(selector).innerHTML = current;\n      if (current >= end) document.querySelector(selector).innerHTML = end;\n      if (current >= end) clearInterval(timer);\n    }, Math.abs(Math.floor(duration \/ (end - start))));\n  return timer;\n};\n\/\/ Example\ncounter('#my-id', 1, 1000, 5, 2000); \/\/ \u4e3a id=\"my-id\" \u7684\u5143\u7d20\u521b\u5efa\u4e00\u4e2a\u4e24\u79d2\u7684\u8ba1\u65f6\u5668<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001\u526a\u8d34\u677f\u3001\u6807\u7b7e\u9875\u4e0e\u76ee\u5f55<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">22\u3001\u5982\u4f55\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u590d\u5236\u5230\u526a\u8d34\u677f\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const copyToClipboard = str => {\n  const el = document.createElement('textarea');\n  el.value = str;\n  el.setAttribute('readonly', '');\n  el.style.position = 'absolute';\n  el.style.left = '-9999px';\n  document.body.appendChild(el);\n  const selected =\n    document.getSelection().rangeCount > 0\n      ? document.getSelection().getRangeAt(0)\n      : false;\n  el.select();\n  document.execCommand('copy');\n  document.body.removeChild(el);\n  if (selected) {\n    document.getSelection().removeAllRanges();\n    document.getSelection().addRange(selected);\n  }\n};\n\/\/ Example\ncopyToClipboard('Lorem ipsum'); \/\/ 'Lorem ipsum' copied to clipboard.<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">23\u3001\u5982\u4f55\u786e\u5b9a\u9875\u9762\u7684\u6d4f\u89c8\u5668\u9009\u9879\u5361\u662f\u5426\u5904\u4e8e\u524d\u53f0\u6d3b\u8dc3\u72b6\u6001\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const isBrowserTabFocused = () => !document.hidden;\n\/\/ Example\nisBrowserTabFocused(); \/\/ true<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">24\u3001\u5982\u679c\u4e00\u4e2a\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5982\u4f55\u521b\u5efa\u5b83\uff1f<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const fs = require('fs');\nconst createDirIfNotExists = dir =>\n  (!fs.existsSync(dir) ? fs.mkdirSync(dir) : undefined);\n\/\/ Example\ncreateDirIfNotExists('test'); \/\/ creates the directory 'test', if it doesn't exist<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u672c\u6587\u5b8c~<\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a24 \u6bb5\u90fd\u662f\u300c\u5c0f\u51fd\u6570 + \u4e00\u884c\u793a\u4f8b\u300d\uff1a\u7528\u5c55\u5f00\u8fd0\u7b97\u7b26\u548c classList\/FormData\/XHR \u628a\u65e5\u5e38 DOM\u3001URL\u3001\u8bf7\u6c42\u95ee\u9898\u6536\u6210\u53ef\u590d\u5236\u7684\u5de5\u5177\u51fd\u6570\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u57fa\u4e8e\u5b9e\u9645\u4f7f\u7528\u573a\u666f\u603b\u7ed3\u4e86 24 \u4e2a ES6 \u4ee3\u7801\u6bb5\uff0c\u53ef\u7528\u6765\u89e3\u51b3\u9879\u76ee\u91cc\u4e00\u6279\u5e38\u89c1\u95ee\u9898\uff1a\u663e\u9690 DOM\u3001\u6eda\u52a8\u3001U [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[109,105],"tags":[150],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3117"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3117"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3117\/revisions"}],"predecessor-version":[{"id":3688,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3117\/revisions\/3688"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}