');display:flex;inset:0;justify-content:center;position:absolute}.copy-to-clipboard-button[data-copy-state=copy-success]:before{content:url('data:image/svg+xml;utf8,')}.kg-bookmark-card figcaption,.kg-gallery-card figcaption,.kg-image-card figcaption,.kg-video-card figcaption{padding-top:2px;text-align:center;--tw-text-opacity:1;color:rgb(71 85 105/var(--tw-text-opacity,1))}.kg-bookmark-card figcaption:is(.dark *),.kg-gallery-card figcaption:is(.dark *),.kg-image-card figcaption:is(.dark *),.kg-video-card figcaption:is(.dark *){--tw-text-opacity:1;color:rgb(203 213 225/var(--tw-text-opacity,1))}.kg-code-card figcaption{padding-top:2px;text-align:center;--tw-text-opacity:1;color:rgb(71 85 105/var(--tw-text-opacity,1))}.kg-code-card figcaption:is(.dark *){--tw-text-opacity:1;color:rgb(15 23 42/var(--tw-text-opacity,1))}.toc{overflow-y:auto}.toc>.toc-list{overflow:hidden;position:relative}.toc>.toc-list li{list-style:none}.js-toc{overflow-y:hidden}.toc-list{margin:0;padding-left:1.5rem}a.toc-link{color:currentColor;height:100%}.is-collapsible{max-height:1000px;overflow:hidden;transition:all .3s ease-in-out}.is-collapsed{max-height:0}.is-position-fixed{position:fixed!important;top:0}.is-active-link{font-weight:700}.toc-link:before{background-color:#eee;content:" ";display:inline-block;height:inherit;left:0;margin-top:-1px;position:absolute;width:2px}.is-active-link:before{background-color:#54bc4b}.toc-list-item{font-size:.95rem;margin-bottom:.5rem}.toc-link{border-radius:.25rem;color:#333;display:block;padding:.25rem .5rem;text-decoration:none;transition:color .2s ease}.toc-link:hover{background-color:#f3f4f6;color:#1e40af}.toc-link.is-active-link{background-color:#e0e7ff;color:#1e40af;font-weight:600}.toc-link.node-name--H2{font-size:1rem}.content img[width="20"],.content img[width="25"]{display:inline;margin-left:.25rem;margin-right:.25rem}.content table td[colspan="2"]{text-align:center}.content table td img{margin:auto}.first-letter\:uppercase:first-letter{text-transform:uppercase}.hover\:scale-110:hover{--tw-scale-x:1.1;--tw-scale-y:1.1;transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))}.hover\:border-blue-400:hover{--tw-border-opacity:1;border-color:rgb(96 165 250/var(--tw-border-opacity,1))}.hover\:border-secondary-300:hover{--tw-border-opacity:1;border-color:rgb(203 213 225/var(--tw-border-opacity,1))}.hover\:bg-blue-700:hover{--tw-bg-opacity:1;background-color:rgb(29 78 216/var(--tw-bg-opacity,1))}.hover\:bg-gray-100:hover{--tw-bg-opacity:1;background-color:rgb(243 244 246/var(--tw-bg-opacity,1))}.hover\:bg-gray-50:hover{--tw-bg-opacity:1;background-color:rgb(249 250 251/var(--tw-bg-opacity,1))}.hover\:bg-primary-100:hover{--tw-bg-opacity:1;background-color:rgb(224 242 254/var(--tw-bg-opacity,1))}.hover\:bg-primary-200:hover{--tw-bg-opacity:1;background-color:rgb(186 230 253/var(--tw-bg-opacity,1))}.hover\:bg-primary-50:hover{--tw-bg-opacity:1;background-color:rgb(240 249 255/var(--tw-bg-opacity,1))}.hover\:bg-primary-50\/80:hover{background-color:rgba(240,249,255,.8)}.hover\:bg-primary-700:hover{--tw-bg-opacity:1;background-color:rgb(29 78 216/var(--tw-bg-opacity,1))}.hover\:bg-red-100:hover{--tw-bg-opacity:1;background-color:rgb(254 226 226/var(--tw-bg-opacity,1))}.hover\:bg-secondary-100:hover{--tw-bg-opacity:1;background-color:rgb(241 245 249/var(--tw-bg-opacity,1))}.hover\:from-blue-600:hover{--tw-gradient-from:#2563eb var(--tw-gradient-from-position);--tw-gradient-to:rgba(37,99,235,0) var(--tw-gradient-to-position);--tw-gradient-stops:var(--tw-gradient-from),var(--tw-gradient-to)}.hover\:to-blue-700:hover{--tw-gradient-to:#1d4ed8 var(--tw-gradient-to-position)}.hover\:text-accent-600:hover{--tw-text-opacity:1;color:rgb(217 119 6/var(--tw-text-opacity,1))}.hover\:text-blue-600:hover{--tw-text-opacity:1;color:rgb(37 99 235/var(--tw-text-opacity,1))}.hover\:text-blue-800:hover{--tw-text-opacity:1;color:rgb(30 64 175/var(--tw-text-opacity,1))}.hover\:text-gray-400:hover{--tw-text-opacity:1;color:rgb(156 163 175/var(--tw-text-opacity,1))}.hover\:text-gray-700:hover{--tw-text-opacity:1;color:rgb(55 65 81/var(--tw-text-opacity,1))}.hover\:text-green-600:hover{--tw-text-opacity:1;color:rgb(22 163 74/var(--tw-text-opacity,1))}.hover\:text-primary-600:hover{--tw-text-opacity:1;color:rgb(37 99 235/var(--tw-text-opacity,1))}.hover\:text-primary-700:hover{--tw-text-opacity:1;color:rgb(29 78 216/var(--tw-text-opacity,1))}.hover\:text-secondary-700:hover{--tw-text-opacity:1;color:rgb(51 65 85/var(--tw-text-opacity,1))}.hover\:opacity-90:hover{opacity:.9}.hover\:shadow-lg:hover{--tw-shadow:0 10px 15px -3px rgba(0,0,0,.1),0 4px 6px -4px rgba(0,0,0,.1);--tw-shadow-colored:0 10px 15px -3px var(--tw-shadow-color),0 4px 6px -4px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow,0 0 #0000),var(--tw-ring-shadow,0 0 #0000),var(--tw-shadow)}.focus\:border-transparent:focus{border-color:transparent}.focus\:bg-primary-50:focus{--tw-bg-opacity:1;background-color:rgb(240 249 255/var(--tw-bg-opacity,1))}.focus\:text-primary-600:focus{--tw-text-opacity:1;color:rgb(37 99 235/var(--tw-text-opacity,1))}.focus\:outline-none:focus{outline:2px solid transparent;outline-offset:2px}.focus\:ring-1:focus{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow,0 0 #0000)}.focus\:ring-blue-500:focus{--tw-ring-opacity:1;--tw-ring-color:rgb(59 130 246/var(--tw-ring-opacity,1))}.focus\:ring-gray-500:focus{--tw-ring-opacity:1;--tw-ring-color:rgb(107 114 128/var(--tw-ring-opacity,1))}.focus\:ring-offset-2:focus{--tw-ring-offset-width:2px}.group:focus-within .group-focus-within\:opacity-100{opacity:1}.group:hover .group-hover\:visible,.group\/level2:hover .group-hover\/level2\:visible{visibility:visible}.group:hover .group-hover\:block{display:block}.group\/item:hover .group-hover\/item\:translate-x-1{--tw-translate-x:0.25rem}.group\/item:hover .group-hover\/item\:translate-x-1,.group\/level2:hover .group-hover\/level2\:translate-x-0{transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))}.group\/level2:hover .group-hover\/level2\:translate-x-0{--tw-translate-x:0px}.group\/level2:hover .group-hover\/level2\:translate-x-1{--tw-translate-x:0.25rem}.group:hover .group-hover\:-translate-x-1,.group\/level2:hover .group-hover\/level2\:translate-x-1{transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))}.group:hover .group-hover\:-translate-x-1{--tw-translate-x:-0.25rem}.group:hover .group-hover\:translate-x-1{--tw-translate-x:0.25rem}.group:hover .group-hover\:rotate-180,.group:hover .group-hover\:translate-x-1{transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))}.group:hover .group-hover\:rotate-180{--tw-rotate:180deg}.group\/bookmark:hover .group-hover\/bookmark\:fill-current,.group\/comment:hover .group-hover\/comment\:fill-current,.group\/like:hover .group-hover\/like\:fill-current{fill:currentColor}.group:hover .group-hover\:fill-black\/75{fill:rgba(0,0,0,.75)}.group:hover .group-hover\:fill-current{fill:currentColor}.group:hover .group-hover\:text-primary-600{--tw-text-opacity:1;color:rgb(37 99 235/var(--tw-text-opacity,1))}.group:hover .group-hover\:opacity-100,.group\/level2:hover .group-hover\/level2\:opacity-100{opacity:1}.dark\:border-primary-400:is(.dark *){--tw-border-opacity:1;border-color:rgb(56 189 248/var(--tw-border-opacity,1))}.dark\:border-secondary-200:is(.dark *){--tw-border-opacity:1;border-color:rgb(226 232 240/var(--tw-border-opacity,1))}.dark\:border-secondary-600:is(.dark *){--tw-border-opacity:1;border-color:rgb(71 85 105/var(--tw-border-opacity,1))}.dark\:border-secondary-700:is(.dark *){--tw-border-opacity:1;border-color:rgb(51 65 85/var(--tw-border-opacity,1))}.dark\:bg-blue-900\/20:is(.dark *){background-color:rgba(30,58,138,.2)}.dark\:bg-gray-800:is(.dark *){--tw-bg-opacity:1;background-color:rgb(31 41 55/var(--tw-bg-opacity,1))}.dark\:bg-gray-800\/95:is(.dark *){background-color:rgba(31,41,55,.95)}.dark\:bg-gray-900:is(.dark *){--tw-bg-opacity:1;background-color:rgb(17 24 39/var(--tw-bg-opacity,1))}.dark\:bg-gray-900\/80:is(.dark *){background-color:rgba(17,24,39,.8)}.dark\:bg-primary-900:is(.dark *){--tw-bg-opacity:1;background-color:rgb(30 58 138/var(--tw-bg-opacity,1))}.dark\:bg-secondary-700:is(.dark *){--tw-bg-opacity:1;background-color:rgb(51 65 85/var(--tw-bg-opacity,1))}.dark\:bg-secondary-800:is(.dark *){--tw-bg-opacity:1;background-color:rgb(30 41 59/var(--tw-bg-opacity,1))}.dark\:bg-secondary-900:is(.dark *){--tw-bg-opacity:1;background-color:rgb(15 23 42/var(--tw-bg-opacity,1))}.dark\:bg-slate-400:is(.dark *){--tw-bg-opacity:1;background-color:rgb(148 163 184/var(--tw-bg-opacity,1))}.dark\:fill-white\/60:is(.dark *){fill:hsla(0,0%,100%,.6)}.dark\:fill-white\/70:is(.dark *){fill:hsla(0,0%,100%,.7)}.dark\:stroke-white\/60:is(.dark *){stroke:hsla(0,0%,100%,.6)}.dark\:text-\[rgba\(255\2c 255\2c 255\2c 0\.5\)\]:is(.dark *){color:hsla(0,0%,100%,.5)}.dark\:text-blue-400:is(.dark *){--tw-text-opacity:1;color:rgb(96 165 250/var(--tw-text-opacity,1))}.dark\:text-gray-100:is(.dark *){--tw-text-opacity:1;color:rgb(243 244 246/var(--tw-text-opacity,1))}.dark\:text-gray-200:is(.dark *){--tw-text-opacity:1;color:rgb(229 231 235/var(--tw-text-opacity,1))}.dark\:text-gray-300:is(.dark *){--tw-text-opacity:1;color:rgb(209 213 219/var(--tw-text-opacity,1))}.dark\:text-gray-400:is(.dark *){--tw-text-opacity:1;color:rgb(156 163 175/var(--tw-text-opacity,1))}.dark\:text-primary-400:is(.dark *){--tw-text-opacity:1;color:rgb(56 189 248/var(--tw-text-opacity,1))}.dark\:text-secondary-100:is(.dark *){--tw-text-opacity:1;color:rgb(241 245 249/var(--tw-text-opacity,1))}.dark\:text-secondary-300:is(.dark *){--tw-text-opacity:1;color:rgb(203 213 225/var(--tw-text-opacity,1))}.dark\:text-secondary-400:is(.dark *){--tw-text-opacity:1;color:rgb(148 163 184/var(--tw-text-opacity,1))}.dark\:text-secondary-600:is(.dark *){--tw-text-opacity:1;color:rgb(71 85 105/var(--tw-text-opacity,1))}.dark\:text-slate-200:is(.dark *){--tw-text-opacity:1;color:rgb(226 232 240/var(--tw-text-opacity,1))}.dark\:text-white:is(.dark *){--tw-text-opacity:1;color:rgb(255 255 255/var(--tw-text-opacity,1))}.dark\:shadow-gray-800\/50:is(.dark *){--tw-shadow-color:rgba(31,41,55,.5);--tw-shadow:var(--tw-shadow-colored)}.dark\:shadow-gray-900\/50:is(.dark *){--tw-shadow-color:rgba(17,24,39,.5);--tw-shadow:var(--tw-shadow-colored)}.dark\:ring-gray-700:is(.dark *){--tw-ring-opacity:1;--tw-ring-color:rgb(55 65 81/var(--tw-ring-opacity,1))}.dark\:ring-secondary-600:is(.dark *){--tw-ring-opacity:1;--tw-ring-color:rgb(71 85 105/var(--tw-ring-opacity,1))}.dark\:hover\:border-secondary-600:hover:is(.dark *){--tw-border-opacity:1;border-color:rgb(71 85 105/var(--tw-border-opacity,1))}.dark\:hover\:bg-gray-700:hover:is(.dark *){--tw-bg-opacity:1;background-color:rgb(55 65 81/var(--tw-bg-opacity,1))}.dark\:hover\:bg-gray-700\/80:hover:is(.dark *){background-color:rgba(55,65,81,.8)}.dark\:hover\:bg-gray-800:hover:is(.dark *){--tw-bg-opacity:1;background-color:rgb(31 41 55/var(--tw-bg-opacity,1))}.dark\:hover\:bg-secondary-700:hover:is(.dark *){--tw-bg-opacity:1;background-color:rgb(51 65 85/var(--tw-bg-opacity,1))}.dark\:hover\:text-accent-400:hover:is(.dark *){--tw-text-opacity:1;color:rgb(251 191 36/var(--tw-text-opacity,1))}.dark\:hover\:text-gray-200:hover:is(.dark *){--tw-text-opacity:1;color:rgb(229 231 235/var(--tw-text-opacity,1))}.dark\:hover\:text-green-400:hover:is(.dark *){--tw-text-opacity:1;color:rgb(74 222 128/var(--tw-text-opacity,1))}.dark\:hover\:text-primary-300:hover:is(.dark *){--tw-text-opacity:1;color:rgb(125 211 252/var(--tw-text-opacity,1))}.dark\:hover\:text-primary-400:hover:is(.dark *){--tw-text-opacity:1;color:rgb(56 189 248/var(--tw-text-opacity,1))}.dark\:hover\:text-secondary-300:hover:is(.dark *){--tw-text-opacity:1;color:rgb(203 213 225/var(--tw-text-opacity,1))}.dark\:focus\:bg-gray-700:focus:is(.dark *){--tw-bg-opacity:1;background-color:rgb(55 65 81/var(--tw-bg-opacity,1))}.dark\:focus\:bg-gray-800:focus:is(.dark *){--tw-bg-opacity:1;background-color:rgb(31 41 55/var(--tw-bg-opacity,1))}.dark\:focus\:text-primary-400:focus:is(.dark *){--tw-text-opacity:1;color:rgb(56 189 248/var(--tw-text-opacity,1))}.dark\:focus\:ring-offset-gray-800:focus:is(.dark *){--tw-ring-offset-color:#1f2937}.dark\:focus\:ring-offset-gray-900:focus:is(.dark *){--tw-ring-offset-color:#111827}.dark\:focus\:ring-offset-secondary-800:focus:is(.dark *){--tw-ring-offset-color:#1e293b}.dark\:focus\:ring-offset-secondary-900:focus:is(.dark *){--tw-ring-offset-color:#0f172a}.group:hover .dark\:group-hover\:fill-white\/75:is(.dark *){fill:hsla(0,0%,100%,.75)}.group:hover .dark\:group-hover\:text-primary-400:is(.dark *){--tw-text-opacity:1;color:rgb(56 189 248/var(--tw-text-opacity,1))}@media (min-width:640px){.sm\:col-span-1{grid-column:span 1/span 1}.sm\:mx-1{margin-left:.25rem;margin-right:.25rem}.sm\:mx-2{margin-left:.5rem;margin-right:.5rem}.sm\:inline{display:inline}.sm\:max-w-screen-sm{max-width:640px}.sm\:grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.sm\:p-8{padding:2rem}.sm\:px-5{padding-left:1.25rem;padding-right:1.25rem}.sm\:px-6{padding-left:1.5rem;padding-right:1.5rem}.sm\:px-8{padding-left:2rem;padding-right:2rem}.sm\:py-10{padding-bottom:2.5rem;padding-top:2.5rem}.sm\:py-3{padding-bottom:.75rem;padding-top:.75rem}}@media (min-width:768px){.md\:col-span-1{grid-column:span 1/span 1}.md\:h-14{height:3.5rem}.md\:h-96{height:24rem}.md\:w-14{width:3.5rem}.md\:grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.md\:grid-cols-4{grid-template-columns:repeat(4,minmax(0,1fr))}.md\:flex-row{flex-direction:row}.md\:text-4xl{font-size:2.25rem;line-height:2.5rem}}@media (min-width:1024px){.lg\:invisible{visibility:hidden}.lg\:absolute{position:absolute}.lg\:left-0{left:0}.lg\:col-span-1{grid-column:span 1/span 1}.lg\:col-span-2{grid-column:span 2/span 2}.lg\:col-span-3{grid-column:span 3/span 3}.lg\:row-span-3{grid-row:span 3/span 3}.lg\:row-span-4{grid-row:span 4/span 4}.lg\:row-span-6{grid-row:span 6/span 6}.lg\:mt-2{margin-top:.5rem}.lg\:block{display:block}.lg\:inline{display:inline}.lg\:flex{display:flex}.lg\:hidden{display:none}.lg\:h-\[496px\]{height:496px}.lg\:w-48{width:12rem}.lg\:grid-cols-3{grid-template-columns:repeat(3,minmax(0,1fr))}.lg\:grid-cols-4{grid-template-columns:repeat(4,minmax(0,1fr))}.lg\:grid-cols-5{grid-template-columns:repeat(5,minmax(0,1fr))}.lg\:grid-cols-\[auto_1fr_auto\]{grid-template-columns:auto 1fr auto}.lg\:flex-row{flex-direction:row}.lg\:items-center{align-items:center}.lg\:gap-2{gap:.5rem}.lg\:rounded-lg{border-radius:.5rem}.lg\:p-6{padding:1.5rem}.lg\:px-8{padding-left:2rem;padding-right:2rem}.lg\:py-2{padding-bottom:.5rem;padding-top:.5rem}.lg\:pl-0{padding-left:0}.lg\:text-3xl{font-size:1.875rem;line-height:2.25rem}.lg\:text-5xl{font-size:3rem;line-height:1}.lg\:opacity-0{opacity:0}.lg\:shadow-lg{--tw-shadow:0 10px 15px -3px rgba(0,0,0,.1),0 4px 6px -4px rgba(0,0,0,.1);--tw-shadow-colored:0 10px 15px -3px var(--tw-shadow-color),0 4px 6px -4px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow,0 0 #0000),var(--tw-ring-shadow,0 0 #0000),var(--tw-shadow)}.lg\:ring-1{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow,0 0 #0000)}.lg\:ring-black{--tw-ring-opacity:1;--tw-ring-color:rgb(0 0 0/var(--tw-ring-opacity,1))}.lg\:ring-opacity-5{--tw-ring-opacity:0.05}.group:focus-within .lg\:group-focus-within\:visible{visibility:visible}.group:hover .lg\:group-hover\:visible{visibility:visible}.group:hover .lg\:group-hover\:opacity-100{opacity:1}.dark\:lg\:shadow-gray-900\/50:is(.dark *){--tw-shadow-color:rgba(17,24,39,.5);--tw-shadow:var(--tw-shadow-colored)}.dark\:lg\:ring-gray-700:is(.dark *){--tw-ring-opacity:1;--tw-ring-color:rgb(55 65 81/var(--tw-ring-opacity,1))}}.gh-post-upgrade-cta{align-items:center;color:#fff;display:flex;flex-direction:column;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif;font-size:16px;text-align:center;width:100%}.gh-post-upgrade-cta-content{border-radius:8px;padding:40px 4vw}.gh-post-upgrade-cta h2{color:#fff;font-size:28px;letter-spacing:-.2px;margin:0;padding:0}.gh-post-upgrade-cta p{margin:20px 0 0;padding:0}.gh-post-upgrade-cta small{font-size:16px;letter-spacing:-.2px}.gh-post-upgrade-cta a{cursor:pointer;font-weight:500}.gh-post-upgrade-cta a,.gh-post-upgrade-cta a:hover{box-shadow:none;color:#fff;text-decoration:underline}.gh-post-upgrade-cta a:hover{opacity:.8}.gh-post-upgrade-cta a.gh-btn{background:#fff;border-radius:4px;display:block;font-size:16px;font-weight:600;margin:28px 0 0;padding:8px 18px;text-decoration:none}.gh-post-upgrade-cta a.gh-btn:hover{opacity:.92}#search.loading{pointer-events:none}#search.loading i,#search.loading span,#search.loading svg{opacity:0;visibility:hidden}#search.loading:before{animation:search-loading-spin .8s linear infinite;border:3px solid rgba(0,0,0,.1);border-radius:50%;border-top-color:currentcolor;height:20px;margin:-10px 0 0 -10px;width:20px;z-index:1}#search.loading:after,#search.loading:before{content:"";left:50%;position:absolute;top:50%}#search.loading:after{animation:search-loading-pulse 1.5s ease-in-out infinite;background:rgba(0,0,0,.05);border-radius:50%;height:32px;margin:-16px 0 0 -16px;width:32px}@keyframes search-loading-spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}@keyframes search-loading-pulse{0%,to{opacity:.5;transform:scale(.8)}50%{opacity:.8;transform:scale(1.2)}}
JavaScript 递归函数在本教程中,您将学习如何使用 JavaScript 递归函数,这是一个函数调用自身的函数。
JavaScript 递归函数介绍
递归函数是一个函数,它会调用自身直到它不调用。这种技术称为递归。
假设您有一个函数名为 recurse()
。如果它在其函数体内调用自身,则它是一个递归函数,如下所示:
function recurse() {
// ...
recurse();
// ...
}
递归函数总是有一个停止调用自身的条件。否则,它将无限地调用自己。所以递归函数通常如下所示:
function recurse() {
if(condition) {
// 停止调用自身
//...
} else {
recurse();
}
}
通常,您使用递归函数将大问题分解为较小的问题。您也会在二叉树和图形等数据结构以及二分搜索和快速排序等算法中找到递归函数。
JavaScript 递归函数示例
让我们举一些使用递归函数的例子。
简单的 JavaScript 递归函数
假设你需要开发一个从指定数倒数到 1 的函数,比如从 3 倒数到 1:
3
2
1
以下是 countDown()
函数大概样子:
function countDown(fromNumber) {
console.log(fromNumber);
}
countDown(3);
这个 countDown(3)
只会浏览器控制台打印数字 3。
要从数字 3 倒数到 1,您可以:
- 打印数字 3。
- 然后调用
countDown(2)
打印数字 2 。 - 最后调用
countDown(1)
打印数字 1 。
以下更改countDown()
为递归函数:
function countDown(fromNumber) {
console.log(fromNumber);
countDown(fromNumber-1);
}
countDown(3);
这将一直运行 countDown(3)
函数,直到超过调用堆栈大小 Uncaught RangeError: Maximum call stack size exceeded. 如下所示:
Uncaught RangeError: Maximum call stack size exceeded.
…因为它没有条件停止调用函数自身。
当下一个数字为零时,countDown
将停止。因此,您添加一个 if 条件,如下所示:
function countDown(fromNumber) {
console.log(fromNumber);
let nextNumber = fromNumber - 1;
if (nextNumber > 0) {
countDown(nextNumber);
}
}
countDown(3);
输出:
3
2
1
似乎 countDown()
按预期工作。
然而,正如函数类型教程中提到的,函数的名称是对实际函数对象的引用。如果在代码中的某处将函数名设置为 null
,递归函数将停止工作。
例如,以下代码将导致错误:
let newYearCountDown = countDown;
// 某些逻辑代码
countDown = null;
// 下面的函数调用将会抛出错误
newYearCountDown(10);
错误:未捕获的类型错误,countDown 不是一个函数。
Uncaught TypeError: countDown is not a function
代码是如何工作的:
- 首先,将
countDown
函数名称分配给变量 newYearCountDown
。 - 其次,将
countDown
函数引用设置为 null
。 - 最后、调用
newYearCountDown
函数。
代码会抛出一个错误,是因为在 countDown()
函数体内将会 countDown
函数名的引用设置 null
,然后又再次通过 countDown()
函数的引用调用 countDown
。
要修复它,您可以使用命名函数表达式,如下所示:
let countDown = function f(fromNumber) {
console.log(fromNumber);
let nextNumber = fromNumber - 1;
if (nextNumber > 0) {
f(nextNumber);
}
}
let newYearCountDown = countDown;
countDown = null;
newYearCountDown(10);
计算 n 个自然数之和
假设您需要使用递归计算从 1 到 n 的自然数之和。您需要 按如下方式定义 sum()
递归函数:
sum(n) = n + sum(n-1)
sum(n-1) = n - 1 + sum(n-2)
...
sum(1) = 1
sum()
递归函数:
function sum(n) {
if (n <= 1) {
return n;
}
return n + sum(n - 1);
}
结论
- 递归函数是一个函数,它会调用自己直到它不调用自己才会终止。
- 递归函数总是有一个条件来阻止函数调用自身。