主宰自己的人生 發表於 2021-8-27 11:33:00

TypeScript 入门指南 【大白话】

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; padding: 0 10px; line-height: 1.6; word-spacing: 0; letter-spacing: 0; word-wrap: break-word; text-align: left; color: rgba(51, 51, 51, 1); font-family: -apple-system, system-ui, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif"><h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">前言</span><span class="suffix"></span></h2>
<h3 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">聊聊为何要学习TypeScript?</span><span class="suffix" style="display: none"></span></h3>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">从开发角度来讲, <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">TypeScript</code> 作为强类型语言,对属性有类型约束。在日常开发中少了减少了不必要的因参数类型造成的<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">BUG</code>,当你在使用同事封装好的函数时,类型不知道怎么传递,在开发<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">TS</code> 时,它会有很友好类型的提示,此时你就知道该函数需要传递哪些参数,并且参数类型是什么类型。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">从项目结构来讲, 使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">TypeScript</code> 可以很好的管控项目,通过建立各个模块的类型管理,通过<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">interface</code> 或者 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">类型断言</code> 来管理项目中类型,在对应的模块使用类型,当项目需要迭代更新时,只需要进行对对应的模块类型进行更改。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">从前端全局观来讲,社区三大框架早已结合<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">TypeScript</code> 了, 社区也很完善,已经过了 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">该不该学习TypeScript 的阶段了</code>。</section></li></ol>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">去年还有很多朋友犹豫用不用学习<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> , 很多被社区朋友发的文章误导, <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 就是 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">AnyScript</code>。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a8836c6231db4bd094394ad433bc29bb~tplv-k3u1fbpfcp-watermark.image"><figcaption style="text-align: center; color: rgba(136, 136, 136, 1); display: none; opacity: 0.6; margin-top: 12px; font-size: 12px">2.png</figcaption></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 对没有接触过 强类型语言的朋友有点学习成本,而且在使用<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 进行开发时,前期可能代码会很臃肿,让你觉得看起来有很多 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">无用的代码</code> , 不过在后期,你就可以感觉到 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 给你带来的乐趣了。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 16px 0 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">学会 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code>也对你在职场竞争有点优势,在跳槽时,假如你已经使用 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 结合 框架 做过一些项目,面试官也会优先考虑你的,薪水从而也提升了。
前端之路还有很长,新的技术/框架更新频率很快, 最终还是离不开 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">JavaScript</code>。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 16px 0 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">下面,我们一起来看看 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code>,本文是对标 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 文档进行来讲解,更加大白话的简单讲述<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">如何使用TypeScript</code> .</p>
<h3 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">入手导图</span><span class="suffix" style="display: none"></span></h3>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6868ec8168ea47d9ae2b2e5310576c39~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="text-align: center; opacity: 0.6; font-size: 12px; display: flex; justify-content: center; align-items: center; width: 100%; margin-top: -35px; background: rgba(0, 0, 0, 0.7); color: rgba(255, 255, 255, 1); line-height: 35px; z-index: 20">reWaP1.png</figcaption></figure>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">TypeScript</span><span class="suffix"></span></h2>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">一,安装环境</span><span class="suffix"></span></h2>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#npm&nbsp;install&nbsp;-g&nbsp;typescript<br></code></pre>
<h3 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">1.1 VSCode 配置自动编译文件</span><span class="suffix" style="display: none"></span></h3>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1.</span>&nbsp;在目录下&nbsp;&nbsp;<br>&nbsp;tsc&nbsp;--init&nbsp;&nbsp;&nbsp;自动生成&nbsp;tsconfig.json&nbsp;<br>&nbsp;tsconfig.json&nbsp;下&nbsp;outdir&nbsp;是输出的路径<br>#<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2.</span>&nbsp;&nbsp;任务---&nbsp;运行任务&nbsp;&nbsp;&nbsp;监视&nbsp;tsconfig.json<br><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">二,基本语法</span><span class="suffix"></span></h2>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.1 数组</span><span class="suffix" style="display: none"></span></h4>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">定义使用</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;第一种定义方法&nbsp;&nbsp;&nbsp;let&nbsp;数组名:类型[]&nbsp;=&nbsp;[]</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">var</span>&nbsp;arr:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>[]&nbsp;=&nbsp;[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>];<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(arr);<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;第二种定义方法&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;数组名:Array[类型]&nbsp;=&nbsp;[]</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">var</span>&nbsp;newArr:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Array</span>&lt;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>&gt;&nbsp;=&nbsp;[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>];<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(newArr)<br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.2 元组</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">它表示 已经 元素的个数和元素类型的数组,各个元素类型可以不一样。</p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">访问元组长度 和 元素</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">var</span>&nbsp;strArr:&nbsp;=&nbsp;[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">22</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>,<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">false</span>]<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(strArr.length)<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(strArr[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">0</span>])<br>#它只能按类型的优先顺序输入内容,否则报错<br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.3 枚举 enum</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">enum</code>类型是对JavaScript标准数据类型的一个补充。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">如果没有给枚举指定索引的话,默认为 0 , 通过<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">枚举对象[索引]</code>可以获取值</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">如果指定了枚举索引为字符串的话,通过<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">枚举.属性</code>获取的 它的值</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">enum</span>&nbsp;Sex&nbsp;{Man,Woman}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;m:Sex&nbsp;=&nbsp;Sex.Man;<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(m)&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//0</span><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;w:&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">string</span>&nbsp;=&nbsp;Sex[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span>]<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(w)&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Woman</span><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">enum</span>&nbsp;Animal&nbsp;{Dog&nbsp;=&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>,&nbsp;Cat,&nbsp;Tiger};<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(Animal[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">5</span>])&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Tiger</span><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">enum</span>&nbsp;info&nbsp;{student&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'学生'</span>,&nbsp;teacher&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'教师'</span>,&nbsp;&nbsp;parent&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'家长'</span>&nbsp;};<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(info.teacher)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//教师</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.4 任意类型 any</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">any 为 任意类型,一般在获取dom 使用</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;任意类型</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;newArrs:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">any</span>&nbsp;=&nbsp;[<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试不同数据&nbsp;'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">222</span>,<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">false</span>]<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(newArrs)<br>#&nbsp;输出结果为[&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试不同数据&nbsp;'</span>,&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">222</span>,&nbsp;<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">false</span>&nbsp;]<br>#&nbsp;使用场景:&nbsp;当你不知道类型&nbsp;或&nbsp;一个对象&nbsp;或数据&nbsp;需要多个类型时,使用<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">any</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content"><em style="font-style: italic; color: rgba(0, 0, 0, 1)">2.5 undefined 类型</em></span><span class="suffix" style="display: none"></span></h4>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;num:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>&nbsp;|&nbsp;<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">undefined</span>&nbsp;;<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(num)&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;输出&nbsp;undefined,&nbsp;但是不会报错</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;newNum:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>&nbsp;|&nbsp;<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">undefined</span>&nbsp;=&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">33</span>;<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(newNum)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;输出&nbsp;33&nbsp;</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.6 never 类型</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">never 代表不存在的值类型,常用作为 抛出异常或者 无限循环的函数返回类型</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;应用场景&nbsp;<br>&nbsp;#<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1.</span>&nbsp;抛错误<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;errDate&nbsp;=&nbsp;(message:string):&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">never</span>&nbsp;=&gt;</span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">throw</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Error</span>(message)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;#<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2.</span>&nbsp;&nbsp;死循环<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;date_for&nbsp;=&nbsp;():&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">never</span>&nbsp;=&gt;</span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">while</span>(<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">true</span>)&nbsp;{}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>#&nbsp;never&nbsp;类型是任意类型的子类型,没有类型是never&nbsp;的子类型<br>别的类型不能赋值给never类型,&nbsp;而&nbsp;never&nbsp;类型可以赋值给任意类型<br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.7 void 类型</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">void为 函数没有类型,一般用在没有返回值的函数</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;如果方法类型为<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>,&nbsp;则必须返回内容,&nbsp;内容且必须为数字<br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">add</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">number</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span>&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2323</span>;<br>}<br><br>#&nbsp;如果方法类型为<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">void</span>,不需要返回内容<br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getAdd</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>)<br>}<br><br>#&nbsp;如果方法类型为<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">any</span>,则可以返回任意类型<br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getAny</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">any</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span>&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">999</span>&nbsp;+&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Hello&nbsp;TypeScript'</span><br>}<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(getAny())<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//999&nbsp;'Hello&nbsp;TypeScript'</span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">三,类型断言</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">什么是类型断言?</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">有时候你在定义一个变量时,起初是不知道是什么类型,但在使用过程中知道是什么类型,这时就会用到类型断言了。</section></li></ul>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">3.1第一种写法 尖括号</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;str&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;resLength&nbsp;:&nbsp;number&nbsp;=&nbsp;(<span class="xml" style="line-height: 26px"><span class="hljs-tag" style="line-height: 26px">&lt;<span class="hljs-name" style="color: rgba(228, 86, 73, 1); line-height: 26px">string</span>&gt;</span>str).length<br></span></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">3.2第二种写法as</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;str&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;resLength&nbsp;:&nbsp;number&nbsp;=&nbsp;(str&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">as</span>&nbsp;string).length<br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">四,接口</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">TypeScript的核心原则之一是对值所具有的<em style="font-style: italic; color: rgba(0, 0, 0, 1)">结构</em>进行类型检查。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">验证类型时,顺序不影响验证。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">简单的来说,它是类型约束的定义,当你使用这个定义接口时,它会一一匹对接口中定义的类型。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">只要不满足接口中的任何一个属性,都不会通过的。</p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.1 接口可选属性</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">有时候,接口属性不是必须全部需要的,满足某些条件才会需要,这时,就可以采用可选属性</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">格式 : <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">属性 ?: 类型</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface&nbsp;&nbsp;Login{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">userName</span>:&nbsp;string,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">password</span>:&nbsp;string,<br>&nbsp;&nbsp;&nbsp;&nbsp;auth&nbsp;?:&nbsp;string<br>}<br><br><br><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getLogin</span>(<span class="hljs-params" style="line-height: 26px">obj:&nbsp;Login</span>)&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">if</span>(obj.auth&nbsp;==&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'管理员'</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'可以查看所有菜单'</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">else</span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'您的权限比较低,目前不能查看'</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><br>getLogin({<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">userName</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'zhangsanfeng'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">password</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'12121121sd'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">auth</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'管理员'</span><br>})&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//可以查看所有菜单</span><br><br><br>getLogin({<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">userName</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'zhangsanfeng'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">password</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'12121121sd'</span><br>})&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//您的权限比较低,目前不能查看</span><br><br><br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.2 接口 只读属性</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">只读属性: 意味着给属性赋值了后,不可改变。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">格式: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">readonly 属性 : 类型</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface&nbsp;Menus&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;readonly&nbsp;title?:string,<br>&nbsp;&nbsp;&nbsp;&nbsp;icon?:string,<br>&nbsp;&nbsp;&nbsp;&nbsp;readonly&nbsp;path?:string,<br>&nbsp;&nbsp;&nbsp;&nbsp;readonly&nbsp;Layout?:string<br>}<br><br><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getMenuInfo</span>(<span class="hljs-params" style="line-height: 26px">data:Menus</span>)</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(data)<br>&nbsp;&nbsp;&nbsp;&nbsp;data.icon&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'修改图标'</span>&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;可以修改</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;data.path&nbsp;=&nbsp;'/home'&nbsp;&nbsp;&nbsp;&nbsp;报错,禁止修改,接口属性为只读</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(data)<br>}<br><br><br>getMenuInfo({<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">title</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'主页'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">icon</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'homes'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">path</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'/home'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">Layout</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Layput'</span><br>})<br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.3 接口函数类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">用来约束函数传递参数类型</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">函数类型的类型检查来说,函数的参数名不需要与接口里定义的名字相匹配。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">格式: <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">(参数1: 类型,参数2:类型) : 返回值类型</code></section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;获取用户信息</span><br>interface&nbsp;UserInfo&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;(name:&nbsp;string,<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">adress</span>:&nbsp;string,<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">phone</span>:&nbsp;number)&nbsp;:&nbsp;string<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;getUsefInfo:UserInfo&nbsp;=&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px">name,adress,phone</span>)</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span>&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${name}</span>住在<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${adress}</span>,手机号为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${phone}</span>`</span><br>}<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(getUsefInfo(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'张锋'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'天津南开区xx小区'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">188888888</span>))<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.4 接口可索引类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在定义一个数组时,可以定义一个 索引类型接口,这样就约束了它必须传递哪些类型的值。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">访问: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">通过 变量[索引]</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface&nbsp;Code{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;string<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;errCode&nbsp;:&nbsp;Code&nbsp;=&nbsp;[<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'200&nbsp;成功'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'301&nbsp;重定向'</span>,&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'400&nbsp;客户端错误'</span>,&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'500&nbsp;&nbsp;服务端出错'</span>]<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(errCode[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>])&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//500&nbsp;&nbsp;服务端出错</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.5 类型接口实现</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">接口描述了类的公共部分,而不是公共和私有两部分。 它不会帮你检查类是否具有某些私有成员。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface&nbsp;Animals&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">eye</span>:&nbsp;number,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">leg</span>:&nbsp;number,&nbsp;&nbsp;<br>}<br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Dog</span>&nbsp;&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Animals</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">eye</span>:&nbsp;number;<br>&nbsp;&nbsp;&nbsp;&nbsp;leg:&nbsp;number;<br>&nbsp;&nbsp;&nbsp;&nbsp;kind:&nbsp;string<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(eye:&nbsp;number,&nbsp;leg:&nbsp;number,&nbsp;kind:&nbsp;string)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.eye&nbsp;=&nbsp;eye<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.leg&nbsp;=&nbsp;leg<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.kind&nbsp;=&nbsp;kind<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getDogInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`品种为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.kind}</span>,有<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.eye}</span>只眼,<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.leg}</span>条腿`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;hashiqi&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;Dog(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">4</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'哈士奇'</span>);<br>hashiqi.getDogInfo()&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//品种为哈士奇,有2只眼,4条腿</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.6 接口继承(多合一)</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">接口之间可以互相继承,这样可以更灵活地将接口分割到可重用的模块里。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface&nbsp;Shape1&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">data</span>:&nbsp;string<br>}<br><br>interface&nbsp;Shape2&nbsp;&nbsp;extends&nbsp;Shape1{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>:&nbsp;number<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;Shape2&nbsp;具有&nbsp;Shape1&nbsp;的特征</span><br>}<br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Message</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Shape2</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>:&nbsp;number;<br>&nbsp;&nbsp;&nbsp;&nbsp;data:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(code&nbsp;:&nbsp;number,data:&nbsp;string)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.code&nbsp;=&nbsp;code;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.data&nbsp;=&nbsp;data<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getCodeInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`状态码为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.code}</span>,返回信息为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.data}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;err&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;Message(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">500</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'服务端错误'</span>)<br>err.getCodeInfo()&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//状态码为500,返回信息为服务端错误</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.7 接口继承类</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当接口继承了一个类,那么接口也会拥有类的属性和方法。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当别的类实现这个 接口时,会同时实现 接口的属性和方法, 继承类的属性和方法</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Log</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">time</span>:&nbsp;string&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'2020-11-2'</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getLog(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br>interface&nbsp;&nbsp;Shape3&nbsp;&nbsp;extends&nbsp;Log{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span>&nbsp;:&nbsp;string<br>}<br><br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">ErrLog</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Shape3</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span>:&nbsp;string&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;time:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(message:&nbsp;string,&nbsp;time:&nbsp;string)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.message&nbsp;=&nbsp;message;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.time&nbsp;=&nbsp;time<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getLog():&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"Method&nbsp;not&nbsp;implemented."</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;errs&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;ErrLog(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'2020-11-2'</span>)<br>errs.getLog()&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Method&nbsp;not&nbsp;implemented.</span><br><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">五,泛型</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">接触过JAVA 的同学,应该对这个不陌生,非常熟了。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">作为前端的我们,可能第一 次听这个概念。通过 字面意思可以看出,它指代的类型比较广泛。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">作用:: 避免重复代码,代码冗余</section></li></ul>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">但是它和 any类型 还是有区别的。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">any类型:如果一个函数类型为any,那么它的参数可以是任意类型,一般传入的类型与返回的类型应该是相同的。如果传入了一个 string 类型的参数,那么我们也不知道它返回啥类型。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">泛型 : 它可以使<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">返回类型 和 传入类型</code> 保持一致,这样我们可以清楚的知道函数返回的类型为什么类型。</section></li></ul>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">5.1 泛型接口</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">泛型接口可以这样理解:</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当你需要给接口指定类型时,但目前不知道属性类型为什么时,就可以采用泛型接口</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">你可以给接口指定参数为多个泛型类型,也可以单个;当使用时,明确参数类型即可。</p>
</blockquote>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ecd9531f6b6942c8bce86ff798ea8894~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="text-align: center; opacity: 0.6; font-size: 12px; display: flex; justify-content: center; align-items: center; width: 100%; margin-top: -35px; background: rgba(0, 0, 0, 0.7); color: rgba(255, 255, 255, 1); line-height: 35px; z-index: 20">reJsYR.png</figcaption></figure>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">&nbsp;interface&nbsp;User&nbsp;&lt;T,S,Y&gt;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:&nbsp;T;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hobby:&nbsp;S;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age:&nbsp;Y;<br>&nbsp;}<br><br>&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">People</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">User</span>&lt;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">String</span>,<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">String</span>,<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Number</span>&gt;&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">String</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hobby:&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">String</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age:&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Number</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string,hobby:string,age:number){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.hobby&nbsp;=&nbsp;hobby;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age&nbsp;=&nbsp;age;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name+<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"------------------"</span>+<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.hobby)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>的年龄为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<br>&nbsp;}<br>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;xiaoZhou&nbsp;=&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;People(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小周'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'敲代码'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">22</span>)<br>&nbsp;xiaoZhou.getInfo()&nbsp;<br>&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//小周------------------敲代码</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;小周的年龄为22</span><br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">5.2 泛型函数</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">定义泛型函数,可以让 传入参数类型参数 和 返回值类型保持一致。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">泛型 标志一般用字母大写,T 可以随意更换</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">格式&nbsp;:&nbsp;&nbsp;函数名&lt;T&gt;&nbsp;(参数1:T)&nbsp;:&nbsp;返回值类型&nbsp;T<br></code></pre>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">genericity</span>&lt;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">T</span>&gt;&nbsp;(<span class="hljs-params" style="line-height: 26px">data:&nbsp;T</span>)&nbsp;:&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">T</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(data)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span>&nbsp;data<br>}<br><br>genericity(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"测试"</span>)<br>genericity(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">666</span>)<br>genericity([<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'前端'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'后端'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'云端'</span>])<br><br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">5.3 泛型类</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">什么是泛型类</section></li></ol>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">它规定了类中属性和方法的 类型,而且必须和类型定义的类型保持一致。</p>
<ol start="2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">泛型类的作用</section></li></ol>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">可以帮助我们确认类的所有属性都在使用相同的类型</p>
<ol start="3" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">使用格式</section></li></ol>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;类名&lt;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">T</span>&gt;&nbsp;</span>{<br>&nbsp;name!:&nbsp;T;<br>&nbsp;hobby!:&nbsp;T;<br>}<br><br>#&nbsp;这样这个类的所有类型为&nbsp;number<br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;实例&nbsp;=&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;类名&lt;number&gt;();<br><br></code></pre>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;GenericityA&lt;X&gt;{<br>&nbsp;&nbsp;&nbsp;&nbsp;sex!:&nbsp;X;<br>&nbsp;&nbsp;&nbsp;&nbsp;age!:&nbsp;X;<br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;gen&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;GenericityA&lt;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>&gt;();<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;gen.sex&nbsp;=&nbsp;'测试'&nbsp;&nbsp;&nbsp;报错</span><br>gen.age&nbsp;=&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(gen.age)<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">5.4 泛型约束</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">接口约束</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过定义接口, 泛型函数继承接口,则参数必须实现接口中的属性,这样就达到了泛型函数的约束</section></li></ul>
<ol start="2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">类约束</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过给类的泛型指定为另一个类,这样就规定了类泛型的类型都为另一个类</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;第一种<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;定义接口</span><br>&nbsp;interface&nbsp;DataInfo{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">title</span>:&nbsp;string,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">price</span>:&nbsp;number<br>&nbsp;}<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;泛型函数&nbsp;继承接口,进行对参数类型约束,&nbsp;如果传入的参数中,没有包含接口属性,则编译不通过</span><br>&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getDataInfos</span>&lt;&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">T</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">extends</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">DataInfo</span>&gt;&nbsp;(<span class="hljs-params" style="line-height: 26px">obj:&nbsp;T</span>)&nbsp;:&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">T</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span>&nbsp;obj<br>&nbsp;}<br><br>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;book&nbsp;=&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">title</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'前端进阶'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">price</span>:&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">50</span>,&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">author</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小新'</span><br>&nbsp;}<br><br>&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(getDataInfos(book))&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{&nbsp;title:&nbsp;'前端进阶',&nbsp;price:&nbsp;50,&nbsp;author:&nbsp;'小新'&nbsp;}</span><br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;第二种<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;通过类来约束</span><br>&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Login</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">username</span>:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;password:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(username:&nbsp;string,password:string){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.username&nbsp;=&nbsp;username<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.password&nbsp;=&nbsp;password<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;}<br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Mysql</span>&lt;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">T</span>&gt;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;login&lt;T&gt;(info:T):T{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span>&nbsp;info<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;&nbsp;x&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;Login(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'admin'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'12345'</span>);<br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;&nbsp;mysql&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;Mysql&lt;Login&gt;();<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(mysql.login(x))&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Login&nbsp;{&nbsp;username:&nbsp;'admin',&nbsp;password:&nbsp;'12345'&nbsp;}</span><br></code></pre>
<hr data-tool="mdnice编辑器" style="height: 1px; margin: 10px 0; border-top: 1px solid rgba(19, 92, 224, 1); border-right: none; border-bottom: none; border-left: none">
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">六,类 Class</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">说到类,做后端的朋友应该都了解,前端 在ES6 中,才出现了 类 Class 这个关键词。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">Class 有哪些特征</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">属性</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">构造器</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">方法</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">继承<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">extends</code></section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">属性 / 方法 修饰符</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">静态属性</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">抽象类</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">存取器 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">getters/setters</code></section></li></ul>
</blockquote>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">6.1 修饰符</span><span class="suffix" style="display: none"></span></h4>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content"><code>public</code> 共有的</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当属性 / 方法 修饰符为 public 时, 如果前面没有,默认会加上,我们可以自由的访问程序里定义的成员。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Fruit</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;name:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;price:&nbsp;number;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;以上为等价</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:&nbsp;string,&nbsp;price:&nbsp;number)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price&nbsp;=&nbsp;price<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getFruitInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`您要购买的水果为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${name}</span>,价格为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content"><code>private</code> 私有的</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当成员被标记成 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">private</code>时,它就不能在声明它的类的外部访问。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Fruit</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;name:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;price:&nbsp;number;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;以上为等价</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:&nbsp;string,&nbsp;price:&nbsp;number)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price&nbsp;=&nbsp;price<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getFruitInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`您要购买的水果为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${name}</span>,价格为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;apple&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;Fruit(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'苹果'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">22</span>)<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;console.log(apple.price)&nbsp;&nbsp;&nbsp;报错,&nbsp;实例不可以访问私有属性</span><br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content"><code>protected</code>受保护的</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">protected</code>修饰符与 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">private</code>修饰符的行为很相似,但有一点不同, <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">protected</code>成员在派生类中仍然可以访问,不可以通过实例来访问受保护的属性。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">A</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;name&nbsp;:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;age&nbsp;:&nbsp;number;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:&nbsp;string&nbsp;,&nbsp;age:&nbsp;number)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age&nbsp;=&nbsp;age<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getA(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'A'</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">B</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">extends</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">A</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;job&nbsp;:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:&nbsp;string,&nbsp;job:&nbsp;string,age:&nbsp;number)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">super</span>(name,age)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.job&nbsp;=&nbsp;job<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getB(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`B&nbsp;姓名为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>&nbsp;&amp;&amp;&nbsp;年龄为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age}</span>&nbsp;&amp;&amp;&nbsp;职业为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.job}</span>,`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;b&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;B(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小飞'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'前端工程师'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">22</span>)<br>b.getA()&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//A</span><br>b.getB()&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//B&nbsp;姓名为小飞&nbsp;&amp;&amp;&nbsp;年龄为22&nbsp;&amp;&amp;&nbsp;职业为前端工程师,</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;console.log(b.name)&nbsp;&nbsp;报错,访问不了,protected成员只能在派生类中可以访问,不能通过实例来访问。</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">6.2 静态属性</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">类的静态成员(属性 和 方法) 只能通过 类来可以访问。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">定义: static属性   /   static 方法</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Food</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;name:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;price:&nbsp;number;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">static</span>&nbsp;adress:&nbsp;string&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'四川'</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;以上为等价</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:&nbsp;string,&nbsp;price:&nbsp;number)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price&nbsp;=&nbsp;price<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getFruitInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`您要购买的东西为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${name}</span>,价格为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;spicy&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;Food(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'辣条'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>)<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(Food.adress)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//四川</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;console.log(spicy.adress)&nbsp;&nbsp;报错&nbsp;&nbsp;类的实例对象不可以访问&nbsp;类的静态属性。&nbsp;只可以通过类.属性来访问</span><br><br><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">6.3 继承 <code>extend</code></span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">继承的本意很好理解,当子类继承了父类,那么子类就拥有了父类的特征(属性) 和 行为(方法),</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">T</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getNames(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'继承类T'</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">S</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">extends</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">T</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;派生类拥有T属性和方法</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">super</span>(name)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getName(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;&nbsp;ss&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;S(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试继承'</span>)<br>ss.getName()&nbsp;&nbsp;<br>ss.getNames()<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;测试继承</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;继承类T</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">6.4 抽象类</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">抽象类可以包含成员的实现细节。 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">abstract</code>关键字是用于定义抽象类和在抽象类内部定义抽象方法。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">抽象类中的抽象方法不包含具体实现并且必须在派生类中实现。</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">abstract&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">E</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;abstract&nbsp;name:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;abstract&nbsp;speak():<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;abstract&nbsp;play():<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>;<br>}<br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">F</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">E</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;派生类&nbsp;F&nbsp;必须实现&nbsp;抽象类E&nbsp;的方法和属性&nbsp;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;speak():&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'具有聊天功能'</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;play():&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'具有娱乐功能'</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">get</span>(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;f&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;F(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>);<br>f.play()&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//具有娱乐功能</span><br>f.get()&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;测试</span><br>f.speak()&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//具有聊天功能</span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">七,TS 中的函数</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">函数类型包括 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">参数类型 和 返回值类型</code></p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">7.1 函数添加返回值类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">每个参数添加类型之后再为函数本身添加返回值类型.</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">TypeScript能够根据返回语句自动推断出返回值类型,因此我们通常省略它。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">下面会介绍在TS 中,两种写函数的格式</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;第一种写法</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;getInterFaceInfo&nbsp;:&nbsp;<span class="hljs-function" style="line-height: 26px">(<span class="hljs-params" style="line-height: 26px">obj:object</span>)&nbsp;=&gt;</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>&nbsp;=&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px">obj</span>)</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(obj)<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;infos:&nbsp;object&nbsp;=&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>:&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">200</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'发送成功'</span><br>}<br>getInterFaceInfo(infos)<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;第二种写法</span><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getCode</span>(<span class="hljs-params" style="line-height: 26px">code:&nbsp;number,&nbsp;message:string</span>)&nbsp;:&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`code为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${code}</span>,message为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${message}</span>`</span>)<br>}<br><br>getCode(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">200</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'接受成功'</span>)&nbsp;<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">7.2 函数可选参数/默认参数</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">JavaScript里,每个参数都是可选的,可传可不传。 没传参的时候,它的值就是undefined。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在TypeScript里我们可以在参数名旁使用 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">?</code>实现可选参数的功能。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2"><strong style="background: linear-gradient(90deg, rgba(50, 153, 210, 1), rgba(239, 189, 181, 1)); color: rgba(255, 255, 255, 1); font-weight: 400; padding: 0 4px; display: inline-block; border-radius: 4px; margin: 0 2px; letter-spacing: 1px">可选参数必须放在必须参数后面。</strong></section></li></ul>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">格式&nbsp;:&nbsp;函数名(变量名?:类型):类型&nbsp;{}&nbsp;&nbsp;<br></code></pre>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">默认参数,在传递参数时,指定默认值</section></li></ul>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">格式 : 函数名(变量名 :类型 = "xx"):类型 {}
</code></pre>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;可选参数</span><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getNetWork</span>(<span class="hljs-params" style="line-height: 26px">ip:string,domain:string,agreement?:string</span>)</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`ip地址为:<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${ip}</span>,域名为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${domain}</span>,协议为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${agreement}</span>`</span>)<br>}<br><br>getNetWork(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'127.0.0.1'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'www.xiaomi.com'</span>)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//ip地址为:127.0.0.1,域名为www.xiaomi.com,协议为undefined</span><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;默认参数</span><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getNetWorks</span>(<span class="hljs-params" style="line-height: 26px">ip:string,domain:string,agreement:string&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'http'</span></span>)</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`ip地址为:<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${ip}</span>,域名为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${domain}</span>,协议为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${agreement}</span>`</span>)<br>}<br>getNetWorks(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'127.0.0.1'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'www.xiaomi.com'</span>)&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//ip地址为:127.0.0.1,域名为www.xiaomi.com,协议为http&nbsp;&nbsp;&nbsp;&nbsp;</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">7.3 函数剩余参数</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">有时,你想同时操作多个参数,或者你并不知道会有多少参数传递进来。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在JavaScript里,你可以使用 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">arguments</code>来访问所有传入的参数。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在TypeScript 中,可以把所有参数集中在一个变量中,前面加上<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">...</code> 表示 剩余参数。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">注意</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">直接通过变量访问</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">也可以通过索引访问</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">只能定义一个剩余参数,且位置在 默认参数和可选参数后面</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getNumberInfo</span>(<span class="hljs-params" style="line-height: 26px">num:number,...peopleArray:&nbsp;string&nbsp;[]</span>)&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`人员个数为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${num}</span>,成员为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${peopleArray}</span>`</span>)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;也可以通过索引来获取元素</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`人员个数为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${num}</span>,成员为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${peopleArray[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span>]}</span>`</span>)&nbsp;<br>}<br><br>getNumberInfo(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">4</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小明'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小李'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小红'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小张'</span>)&nbsp;&nbsp;<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//人员个数为4,成员为小明,小李,小红,小张</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//人员个数为4,成员为小李</span><br></code></pre>
<hr data-tool="mdnice编辑器" style="height: 1px; margin: 10px 0; border-top: 1px solid rgba(19, 92, 224, 1); border-right: none; border-bottom: none; border-left: none">
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">八,枚举</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">枚举可以清晰地表达一组对应关系。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">TypeScript支持数字的和基于字符串的枚举。</p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">8.1 数字枚举</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">默认枚举的顺序以 0 开头,然后自动递增。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">枚举顺序也可以指定 值, 指定后,它前面第一个还是以0 递增</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">访问</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">枚举名.属性</code>访问到的是 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">序号</code></section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">枚举名[序号]</code>访问到的是 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">属性名</code></section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">enum&nbsp;Sex&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;x,<br>&nbsp;&nbsp;&nbsp;&nbsp;man&nbsp;=&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">4</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;woman&nbsp;<br>}<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(Sex.x)&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//0</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`小红的性别为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${Sex[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">5</span>]}</span>`</span>)&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//小红的性别为woman</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`后端接受小红的性别ID&nbsp;<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${Sex.woman}</span>`</span>)&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//后端接受小红的性别ID&nbsp;5</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">8.2 字符串枚举</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">enum&nbsp;Job&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;frontEnd&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'前端'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;backEnd&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'后端'</span><br>}<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(Job)&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{&nbsp;frontEnd:&nbsp;'前端',&nbsp;backEnd:&nbsp;'后端'&nbsp;}&nbsp;</span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">九,高级类型</span><span class="suffix"></span></h2>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">9.1 交叉类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">它指 可以将多个类型合并为一个类型。标识符为 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">&amp;</code> , 当指定一个变量类型为 交叉类型时,那么它拥有交叉类型的所有属性,也就是并集。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface&nbsp;DonInterface&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;run():<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>;<br>}<br>interface&nbsp;CatInterface&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;jump():<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>;<br>}<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//这里的pet将两个类型合并,所以pet必须保护两个类型所定义的方法</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;pet&nbsp;:&nbsp;DonInterface&nbsp;&amp;&nbsp;CatInterface&nbsp;=&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">run</span>:<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px"></span>)</span>{},<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">jump</span>:<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px"></span>)</span>{}<br>}<br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">9.2 联合类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">联合类型表示一个值可以是几种类型之一。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">用竖线( <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">|</code>)分隔每个类型。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">一个值是联合类型,我们只能访问此联合类型的所有类型里共有的成员。</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getMenus</span>(<span class="hljs-params" style="line-height: 26px">info:&nbsp;string&nbsp;|&nbsp;number</span>)&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(info)<br>&nbsp;}<br><br><br>getMenus(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"测试"</span>)<br>getMenus(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span>)<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;getMenus(false)&nbsp;&nbsp;报错</span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">十,模块</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">模块: 定义的变量,函数,类等等,只能在自身的作用域里使用。 如果想在外部访问使用,那么必须使用<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export</code> 将其导出即可。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">使用模块: 通过 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">import</code>将模块内容导入即可使用。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">模块是自声明的;两个模块之间的关系是通过在文件级别上使用imports和exports建立的。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">模块使用模块加载器去导入其它的模块。 在运行时,模块加载器的作用是在执行此模块代码前去查找并执行这个模块的所有依赖。</section></li></ul>
</blockquote>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.导出</span><span class="suffix" style="display: none"></span></h4>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.1 导出声明</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">任何声明(比如变量,函数,类,类型别名或接口)都能够通过添加<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export</code>关键字来导出。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">导出可以对任何声明 进行重命名,防止命名冲突, 通过 as 来修改</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;模块A&nbsp;文件<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;导出接口</span><br>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;interface&nbsp;A&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getList()&nbsp;:&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span><br>&nbsp;}<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;导出变量</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;&nbsp;GET_METHOD&nbsp;=&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"get"</span><br>&nbsp;<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;导出类</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">S</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">A</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;getList():&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"导出类"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>}<br><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getQueryData</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"获取分页数据"</span>)<br>}<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;导出模块&nbsp;变量重命名</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;{&nbsp;getQueryData&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">as</span>&nbsp;getQuery}<br><br><br><br>#&nbsp;文件B<br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">import</span>&nbsp;{getQuery,S,A}&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span>&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'./模块A'</span>;<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;使用模块中的函数</span><br>getQuery()<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;实例模块中类的对象</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;a&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;S();<br>a.getList()&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;输出导出类</span><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;实现模块中的&nbsp;A&nbsp;接口</span><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Y</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">A</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;getList():&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">throw</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Error</span>(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Method&nbsp;not&nbsp;implemented.'</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>}<br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.2 组合模块使用</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">通常一个大的模块是多个子模块组成的。那么我们可以通过 在大的模块中导入多个子模块。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">格式: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export * from "模块"</code></p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">使用组合模块: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">import * as 重命名变量 from ‘组合模块路径’</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;模块C<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;导出变量</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;&nbsp;GET_METHOD&nbsp;=&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"get"</span><br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;模块B<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;str:&nbsp;string&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"B模块"</span><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getContent</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"我是模块B的内容"</span>)<br>}<br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#组合模块<br><br><br>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;&nbsp;res&nbsp;:&nbsp;object&nbsp;=&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>:&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">200</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"请求成功"</span><br>&nbsp;}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getRes</span>(<span class="hljs-params" style="line-height: 26px"></span>):&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(res)<br>&nbsp;}<br><br>&nbsp;<br>&nbsp;#&nbsp;导出子模块<br>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;*&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span>&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"./modulesC"</span><br>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;*&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span>&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"./moduleB"</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.3 使用组合模块</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">import</span>&nbsp;*&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">as</span>&nbsp;T&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span>&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"./modulesA"</span>;<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;C&nbsp;模块中的</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(T.GET_METHOD)<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;B&nbsp;模块中的内容</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(T.str)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//B模块</span><br>T.getContent()&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//我是模块B的内容</span><br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;A&nbsp;模块中的内容</span><br>T.getRes()&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{&nbsp;code:&nbsp;200,&nbsp;message:&nbsp;'请求成功'&nbsp;}&nbsp;</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.4 默认导出</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">每个模块都可以有一个<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">default</code>导出。 默认导出使用 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">default</code>关键字标记;并且一个模块只能够有一个<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">default</code>导出。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#模块<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;interface&nbsp;K&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;birth:string;<br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">default</span>&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Student</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">K</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;birth:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string,birth:string){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.birth&nbsp;=&nbsp;birth;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getStudentInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name+<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.birth)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#文件A<br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">import</span>&nbsp;D,{K}&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span>&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'./modulesD'</span><br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;使用默认导出</span><br>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;d&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;D(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小明'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'1998'</span>)<br>&nbsp;d.getStudentInfo()<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;参数类型为接口K&nbsp;</span><br>&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getMessage</span>(<span class="hljs-params" style="line-height: 26px">obj:&nbsp;K</span>):&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(obj)<br>&nbsp;}<br>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;obj&nbsp;=&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"小红"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">birth</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"1998"</span><br>&nbsp;}<br>&nbsp;getMessage(obj);<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">​</p>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.5 <code>export = 和 import = require()</code></span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">CommonJS和AMD的环境里都有一个<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports</code>变量,这个变量包含了一个模块的所有导出内容。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">CommonJS和AMD的<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports</code>都可以被赋值为一个<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">对象</code></p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports 和 export default</code> 用途一样,但是 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export default</code>   语法不能兼容CommonJS和AMD的<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports</code>。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在TypeScript 中,为了达到这样效果,可以这样写:</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">导出: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export =</code>   等于 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports</code></p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">导入: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">import module = require("module")</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;模块<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;相当于默认导出</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;=&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Mongodbs</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">host</span>:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;user:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;password:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;port:number;<br>&nbsp;&nbsp;&nbsp;&nbsp;databaseName:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(host:string,user:string,password:string,port:number,databaseName:string)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.host&nbsp;=&nbsp;host;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.user&nbsp;=&nbsp;user;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.password&nbsp;=&nbsp;password;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.port&nbsp;=&nbsp;port;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.databaseName&nbsp;=&nbsp;databaseName<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;query(table:string){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`select&nbsp;*&nbsp;from&nbsp;<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${table}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#使用模块<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">import</span>&nbsp;MogoDb&nbsp;=&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">require</span>(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"./modulesE"</span>)&nbsp;&nbsp;<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;mogodb&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;MogoDb(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'1270.0.1'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'admin'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'123456'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3006</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'TypeScript'</span>)<br><br>mogodb.query(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Vue'</span>)&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//select&nbsp;*&nbsp;from&nbsp;Vue</span><br><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">十一, 命名空间</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">定义</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">“内部模块”称为“命名空间”</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">“外部模块”称为“模块”</section></li></ul>
<ol start="2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">作用</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">减少命名冲突,将代码组织到一个空间内,便于访问。</section></li></ul>
<ol start="3" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">使用格式</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">namespace 空间名 { }</code>,内部通过<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">export</code> 导出来使用内部成员</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">namespace&nbsp;XiaoXin&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;interface&nbsp;&nbsp;GetData{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;price:&nbsp;number;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getInfo(obj:object):any;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;interface&nbsp;&nbsp;GetMessage&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>:&nbsp;number;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Book</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span>&nbsp;&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">GetData</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:&nbsp;string;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;price:&nbsp;number;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string,price:number){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price&nbsp;=&nbsp;price<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getInfo(obj:&nbsp;object)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">throw</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Error</span>(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"Method&nbsp;not&nbsp;implemented."</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buyBook(obj:&nbsp;GetMessage)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(obj)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;fontEnd&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;&nbsp;XiaoXin.Book(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"前端开发手册"</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">99</span>)<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">var</span>&nbsp;obj&nbsp;=&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>:&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">200</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"购买成功"</span><br>}<br><br>fontEnd.buyBook(obj)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{&nbsp;code:&nbsp;200,&nbsp;message:&nbsp;'购买成功'&nbsp;}</span><br><br><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">test</span>(<span class="hljs-params" style="line-height: 26px">obj:XiaoXin.GetMessage</span>)</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(obj)<br>}<br><br>test(obj)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{&nbsp;code:&nbsp;200,&nbsp;message:&nbsp;'购买成功'&nbsp;}&nbsp;</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">11.1 拆分命名空间</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当应用变得越来越大时,我们需要将代码分离到不同的文件中以便于维护。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">我们可以将命名空间文件拆分成多个文件,但是它们的命名空间名还是使用的同一个,各个文件相互依赖使用。但是必须文件最开头引入 命名空间文件。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">格式:<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">/// &lt;reference path="MS1.ts"/&gt;</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;根命名空间<br>namespace&nbsp;School&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;schoolName&nbsp;=&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"清华大学"</span><br>}<br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;子命名空间<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">///&nbsp;&lt;reference&nbsp;path="MS1.ts"&nbsp;/&gt;</span><br><br>namespace&nbsp;School{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Teacher</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">faculty</span>:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age:number;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(faculty:string,name:string,age:number){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.faculty&nbsp;=&nbsp;faculty;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age&nbsp;=&nbsp;age<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.faculty}</span>,年龄为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSchool(schoole:string){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>老师就职于<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${schoole}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;&nbsp;子命名空间<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">///&nbsp;&nbsp;&lt;reference&nbsp;path="MS1.ts"&nbsp;/&gt;</span><br><br>&nbsp;namespace&nbsp;School{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span>&nbsp;<span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Student</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age:number;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hobby:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string,age:number,hobby:string)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age&nbsp;=&nbsp;age;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.hobby&nbsp;=&nbsp;hobby;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>是一个学生,年龄为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age}</span>,爱好是<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.hobby}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;}<br><br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#&nbsp;使用合并的命名空间<br><br><br>导入命名空间<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">///&nbsp;&lt;reference&nbsp;path="MS1.ts"&nbsp;/&gt;</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">///&nbsp;&lt;reference&nbsp;path="MS2.ts"&nbsp;/&gt;</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">///&nbsp;&lt;reference&nbsp;path="MS4.ts"&nbsp;/&gt;</span><br><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;teacher&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;School.Teacher(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'计算机教授'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'张博士'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">34</span>);<br>teacher.getInfo()&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//张博士为计算机教授,年龄为34</span><br>teacher.getSchool(School.schoolName)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//张博士老师就职于清华大学</span><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;students&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;School.Student(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'张三'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">17</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'玩LOL'</span>);<br>students.getInfo()&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//张三是一个学生,年龄为17,爱好是玩LOL</span><br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">​</p>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">编译命名空间文件</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">第一种方法:&nbsp;会编译为&nbsp;一个js文件<br>tsc&nbsp;--outFile&nbsp;sample.js&nbsp;Test.ts<br><br><br>第二种方法:&nbsp;&nbsp;会编译为多个js文件,然后通过&nbsp;&lt;script&gt;&nbsp;src&nbsp;引入js文件即可<br>tsc&nbsp;--outFile&nbsp;sample.js&nbsp;Validation.ts&nbsp;LettersOnlyValidator.ts&nbsp;ZipCodeValidator.ts&nbsp;Test.ts<br><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">十二,装饰器</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">装饰器是一种特殊类型的声明,它能够附加到类声明、方法、访问符、属性、类方法的参数上,以达到扩展类的行为。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">自从 ES2015 引入 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">class</code>,当我们需要在多个不同的类之间共享或者扩展一些方法或行为的时候,代码会变得错综复杂,极其不优雅,这也是装饰器被提出的一个很重要的原因。</p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">12.1 修饰器分类</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">类装饰器</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">属性装饰器</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">方法装饰器</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">参数装饰器</section></li></ul>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">修饰器写法:&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1.</span>&nbsp;普通修饰器&nbsp;&nbsp;(不传参数)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2.</span>&nbsp;&nbsp;装饰器工厂&nbsp;(传参数)<br></code></pre>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">12.2 类装饰器</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">类装饰器表达式会在运行时当作函数被调用,类的构造函数作为其唯一的参数。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">使用场景:应用于类构造函数,可以用来监视,修改或替换类定义。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span>&nbsp;extension&nbsp;=&nbsp;(<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>:&nbsp;Function):any&nbsp;=&gt;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>.prototype.coreHour&nbsp;=&nbsp;'10:00-15:00'<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>.prototype.meeting&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'重载:Daily&nbsp;meeting!'</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;}<br><br>@extension<br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Employee</span>&nbsp;</span>{<br>&nbsp;&nbsp;public&nbsp;name!:&nbsp;string<br>&nbsp;&nbsp;public&nbsp;department!:&nbsp;string<br><br><br>&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:&nbsp;string,&nbsp;department:&nbsp;string)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.department&nbsp;=&nbsp;department<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;meeting()&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Every&nbsp;Monday!'</span>)<br>&nbsp;&nbsp;}<br><br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;e:&nbsp;any&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;Employee(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'装饰器'</span>,&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>)<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(e)&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Employee&nbsp;{&nbsp;name:&nbsp;'装饰器',&nbsp;department:&nbsp;'测试'&nbsp;}</span><br>&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(e.coreHour)&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;10:00-15:00</span><br>&nbsp;e.meeting()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;重载:Daily&nbsp;meeting!</span><br>&nbsp;&nbsp;<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">12.3 类属性装饰器</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">作用于类属性的装饰器表达式会在运行时当作函数被调用,传入下列3个参数 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">target</code>、<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">name</code>、<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">descriptor</code>:</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">target</code>: 对于静态成员来说是类的构造函数,对于实例成员是类的原型对象</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">name</code>: 成员的名字</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">descriptor</code>: 成员的属性描述符</section></li></ul>
<h4 style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">执行顺序: 当调用有装饰器的函数时,会先执行装饰器,后再执行函数。</span><span class="suffix" style="display: none"></span></h4>
</blockquote>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">通过修饰器完成一个属性只读功能,其实就是修改数据描述符中的 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">writable</code> 的值 :</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>&nbsp;<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">readonly</span>(<span class="hljs-params" style="line-height: 26px">value:&nbsp;boolean</span>)</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span>&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px">target:any,name:string,descriptor:PropertyDescriptor</span>)&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;descriptor.writable&nbsp;=&nbsp;value<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Student</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:string;<br>&nbsp;&nbsp;&nbsp;&nbsp;school:string&nbsp;=&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'社会大学'</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name&nbsp;=&nbsp;name<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;@readonly(<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">false</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;getDataInfo(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>毕业于<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.school}</span>`</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span>&nbsp;sss&nbsp;=&nbsp;<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span>&nbsp;Student(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小李子'</span>)<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;报错,&nbsp;&nbsp;只能读,不能修改</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;sss.getDataInfo&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;{</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log("测试修改")</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//&nbsp;}</span><br><br>sss.getDataInfo()<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">更多精彩文章在主页</p>
<h3 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">最后</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">文中如有错误,欢迎码友在评论区指正,如果对你有所帮助,欢迎点赞和关注~~~
<img src="https://img-hello-world.oss-cn-beijing.aliyuncs.com/imgs/497ff10ccd161003e02b5b4bbdd8548e.png">
</p>
</section><br><br>
来源:https://www.cnblogs.com/HaiJun-Aion/p/15193108.html
頁: [1]
查看完整版本: TypeScript 入门指南 【大白话】